Qt自定义GridView从显示单个到九宫格
一、概述
由于测试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; }
分类:
Qt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2021-12-20 使用C语言获取当前系统的libevent支持的异步IO模型有哪些
2021-12-20 使用C语言实现本地socke通讯
2021-12-20 使用C语言进行udp通信