Qt自定义GridView从显示单个到九宫格

发布时间 2023-12-20 10:55:29作者: 飘杨......

一、概述

  由于测试OpenCV的需要自定义一个可变的用于显示图片的GridView,从显示单张图片到9张图片。效果图如下:

  这个GridView目前只是自己使用,还有瑕疵,这里仅提供一个可行性的思路,有需要可以自行扩展。

二、代码示例

  1.自定义GridView--->VariableGridView.h/VariableGridView.cpp

class VariableGridView : public QWidget
{
    Q_OBJECT

public:
    VariableGridView(QWidget* parent = nullptr);
    ~VariableGridView();
public:
    /**
    * @param mSize item个数
    * @param type 0九宫格 1自定义
    */
    void setViews(int mSize,int type);
    void setAdapter(vector<QPixmap> pixmaps);

private:
    int mWidth;
    int mHeight;
    vector<QLabel*> imageTips;
};
#include "VariableGridView.h"


VariableGridView::VariableGridView(QWidget* parent)
    : QWidget(parent)
{
}

//设置数据源
void VariableGridView::setViews(int mSize, int type) {
    this->mWidth = this->width();
    this->mHeight = this->height();
    for (int i = 0;i < mSize;i++) {
        QLabel* label = new QLabel(this);
        label->setScaledContents(true);
        imageTips.push_back(label);
    }

    if (mSize > 0) {
        switch (mSize) {
        case 1: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            if (type == 1) {
                tip->resize(mWidth, mHeight);
            }
            else {
                tip->resize(mWidth / 2, mHeight / 2);
            }
            tip->move(0, 0);
        }
              break;
        case 2: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            if (type == 1) {
                tip->resize((mWidth - 20) / 2, mHeight);
            }
            else {
                tip->resize((mWidth - 20) / 2, mHeight / 2);
            }

            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            if (type == 1) {
                tip1->resize((mWidth - 20) / 2, mHeight);
            }
            else {
                tip1->resize((mWidth - 20) / 2, mHeight / 2);
            }
            tip1->move(tip->x() + tip->width() + 5, 0);
        }
              break;
        case 3: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 2, mHeight / 2);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 2, mHeight / 2);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 2, mHeight / 2);
            tip2->move(0, tip1->y() + 5 + tip1->height());
        }
              break;
        case 4: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 2, mHeight / 2);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 2, mHeight / 2);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 2, mHeight / 2);
            tip2->move(0, tip1->y() + 5 + tip1->height());
            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(mWidth / 2, mHeight / 2);
            tip3->move(tip2->x() + tip2->width() + 5, tip1->y() + tip1->height() + 5);
        }
              break;
        case 5: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 3, mHeight / 2);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 3, mHeight / 2);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 3, mHeight / 2);
            tip2->move(tip1->x() + tip1->width() + 5, 0);

            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(mWidth / 3, mHeight / 2);
            tip3->move(0, tip->y() + tip->height() + 5);
            QLabel* tip4 = imageTips[4];
            tip4->setScaledContents(true);
            tip4->resize(mWidth / 3, mHeight / 2);
            tip4->move(tip3->x() + tip3->width() + 5, tip->y() + tip->height() + 5);
        }
              break;
        case 6: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 3, mHeight / 2);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 3, mHeight / 2);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 3, mHeight / 2);
            tip2->move(tip1->x() + tip1->width() + 5, 0);

            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(mWidth / 3, mHeight / 2);
            tip3->move(0, tip->y() + tip->height() + 5);
            QLabel* tip4 = imageTips[4];
            tip4->setScaledContents(true);
            tip4->resize(mWidth / 3, mHeight / 2);
            tip4->move(tip3->x() + tip3->width() + 5, tip->y() + tip->height() + 5);
            QLabel* tip5 = imageTips[5];
            tip5->setScaledContents(true);
            tip5->resize(mWidth / 3, mHeight / 2);
            tip5->move(tip4->x() + tip4->width() + 5, tip->y() + tip->height() + 5);
        }
              break;
        case 7: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 3, mHeight / 3);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 3, mHeight / 3);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 3, mHeight / 3);
            tip2->move(tip1->x() + tip1->width() + 5, 0);

            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(mWidth / 3, mHeight / 3);
            tip3->move(0, tip->y() + tip->height() + 5);
            QLabel* tip4 = imageTips[4];
            tip4->setScaledContents(true);
            tip4->resize(mWidth / 3, mHeight / 3);
            tip4->move(tip3->x() + tip3->width() + 5, tip->y() + tip->height() + 5);
            QLabel* tip5 = imageTips[5];
            tip5->setScaledContents(true);
            tip5->resize(mWidth / 3, mHeight / 3);
            tip5->move(tip4->x() + tip4->width() + 5, tip->y() + tip->height() + 5);

            QLabel* tip6 = imageTips[6];
            tip6->setScaledContents(true);
            tip6->resize(mWidth / 3, mHeight / 3);
            tip6->move(0, tip5->y() + tip5->height() + 5);
        }
              break;
        case 8: {
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(mWidth / 3, mHeight / 3);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(mWidth / 3, mHeight / 3);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(mWidth / 3, mHeight / 3);
            tip2->move(tip1->x() + tip1->width() + 5, 0);

            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(mWidth / 3, mHeight / 3);
            tip3->move(0, tip->y() + tip->height() + 5);
            QLabel* tip4 = imageTips[4];
            tip4->setScaledContents(true);
            tip4->resize(mWidth / 3, mHeight / 3);
            tip4->move(tip3->x() + tip3->width() + 5, tip->y() + tip->height() + 5);
            QLabel* tip5 = imageTips[5];
            tip5->setScaledContents(true);
            tip5->resize(mWidth / 3, mHeight / 3);
            tip5->move(tip4->x() + tip4->width() + 5, tip->y() + tip->height() + 5);

            QLabel* tip6 = imageTips[6];
            tip6->setScaledContents(true);
            tip6->resize(mWidth / 3, mHeight / 3);
            tip6->move(0, tip5->y() + tip5->height() + 5);
            QLabel* tip7 = imageTips[7];
            tip7->setScaledContents(true);
            tip7->resize(mWidth / 3, mHeight / 3);
            tip7->move(tip6->x() + tip6->width() + 5, tip5->y() + tip5->height() + 5);
        }
              break;
        case 9: {
            int itemWidth = (mWidth - 50) / 3;
            int itemHeight = (mHeight - 50) / 3;
            QLabel* tip = imageTips[0];
            tip->setScaledContents(true);
            tip->resize(itemWidth, itemHeight);
            tip->move(0, 0);
            QLabel* tip1 = imageTips[1];
            tip1->setScaledContents(true);
            tip1->resize(itemWidth, itemHeight);
            tip1->move(tip->x() + tip->width() + 5, 0);
            QLabel* tip2 = imageTips[2];
            tip2->setScaledContents(true);
            tip2->resize(itemWidth, itemHeight);
            tip2->move(tip1->x() + tip1->width() + 5, 0);

            QLabel* tip3 = imageTips[3];
            tip3->setScaledContents(true);
            tip3->resize(itemWidth, itemHeight);
            tip3->move(0, tip->y() + tip->height() + 5);
            QLabel* tip4 = imageTips[4];
            tip4->setScaledContents(true);
            tip4->resize(itemWidth, itemHeight);
            tip4->move(tip3->x() + tip3->width() + 5, tip->y() + tip->height() + 5);
            QLabel* tip5 = imageTips[5];
            tip5->setScaledContents(true);
            tip5->resize(itemWidth, itemHeight);
            tip5->move(tip4->x() + tip4->width() + 5, tip->y() + tip->height() + 5);

            QLabel* tip6 = imageTips[6];
            tip6->setScaledContents(true);
            tip6->resize(itemWidth, itemHeight);
            tip6->move(0, tip5->y() + tip5->height() + 5);
            QLabel* tip7 = imageTips[7];
            tip7->setScaledContents(true);
            tip7->resize(itemWidth, itemHeight);
            tip7->move(tip6->x() + tip6->width() + 5, tip5->y() + tip5->height() + 5);
            QLabel* tip8 = imageTips[8];
            tip8->setScaledContents(true);
            tip8->resize(itemWidth, itemHeight);
            tip8->move(tip7->x() + tip7->width() + 5, tip5->y() + tip5->height() + 5);
        }
              break;
        }
    }
}

void VariableGridView::setAdapter(vector<QPixmap> pixmaps) {
    for (int i = 0;i < pixmaps.size();i++) {
        this->imageTips[i]->setPixmap(pixmaps[i]);
        //this->imageTips[i]->setItemsPixmap(pixmaps[i]);
    }
}

VariableGridView::~VariableGridView()
{

}

 2.使用

GridViewExampleWindow::GridViewExampleWindow(QWidget* parent)
    : BaseSceneView(parent)
{
    this->setWindowTitle("自定义GridView");
    // 获取主屏幕  
    QScreen* screen = QApplication::primaryScreen();
    if (screen) {
        // 获取屏幕的尺寸  
        QRect screenSize = screen->geometry();
        int width = screenSize.width();
        int height = screenSize.height();
        this->setFixedSize(1000, 480);
        this->setGraphicsViewSize(1000, 480);
        /*this->setFixedSize(width, height);
        this->setGraphicsViewSize(width, height);*/
    }

    //选择图片
    ChoiceImageWidget* choiceImageWidget = new ChoiceImageWidget(this);
    choiceImageWidget->setFixedWidth(this->width() / 5 * 1);
    choiceImageWidget->setCallback([=](QString filePath) {
        this->filePath = filePath;
        qDebug() << "选择图片完成开始打印路径:" << filePath;
        this->execute();
        });

    //MinusPlusWidget* minusPlusWidget = new MinusPlusWidget(this);

    QHBoxLayout* hLayout = new QHBoxLayout(this);
    QVBoxLayout* vLayout = new QVBoxLayout(this);


    vLayout->addWidget(choiceImageWidget);
    //vLayout->addWidget(minusPlusWidget);
    vLayout->setAlignment(Qt::AlignTop);

    QVBoxLayout* vLayout2 = new QVBoxLayout(this);
    gridView = new VariableGridView(this);
    gridView->setStyleSheet("background-color:#000000");
    gridView->resize(QSize(this->width() / 5 * 4, this->height()));
    gridView->setViews(9,1);

    vLayout2->addWidget(gridView);
    hLayout->addLayout(vLayout, 1);
    hLayout->addLayout(vLayout2, 3);

}

QPixmap GridViewExampleWindow::handle() {
    qDebug() << "耗时任务";
    int gvWidth = gridView->width() / 3;
    int gvHeight = gridView->height() / 3;
    Mat src = imread(filePath.toStdString().c_str());
    //cv::resize(src, src, cv::Size(gvWidth, gvHeight));
    vector<QPixmap> pixmaps;
    for (int i = 0;i < 9;i++) {//, gvWidth / 5 * 4, gvHeight / 5 * 4
        pixmaps.push_back(ImageUtils::getPixmap(src.clone()));
    }
    gridView->setAdapter(pixmaps);
    return NULL;
}