一、前言
本文主要运用知识为 1、QTableView控件的代理委托;2、颜色进度条设置;3、openCV的Mat数据的显示问题
本文主要实现功能为:利用QGraphicsView控件显示分类图,并实现相关的放大、缩小、平移等;对QTableView进行委托显示;利用PS绘制进度条,添加资源文件,作为颜色进度条;QGraphicsView显示openCV的Mat数据;双击QtableView设置自定义颜色。
二、QTableView控件委托
运用的是QItemDelegate类,实现功能如下,对控件的的列添加背景色,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "colorDegete.h" 2 #include <QPixmap> 3 #include <QApplication> 4 #include <QRect> 5 #include <QVector> 6 #include <QDebug> 7 8 9 colorDegete::colorDegete(QObject *parent) 10 :QItemDelegate(parent) 11 { 12 13 } 14 15 colorDegete::~colorDegete() 16 { 17 18 } 19 20 void colorDegete::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 21 { 22 QString data = index.data().toString(); 23 QStringList list1 = data.split(",",QString::SkipEmptyParts); 24 int r = list1.at(0).toInt(); 25 int g = list1.at(1).toInt(); 26 int b = list1.at(2).toInt(); 27 QColor color(r,g,b); 28 QRect rect = option.rect; 29 QPixmap pixmap(rect.width(),rect.height()); 30 pixmap.fill(color); 31 QApplication::style()->drawItemPixmap(painter,option.rect,Qt::AlignCenter,QPixmap(pixmap)); 32 }
三、颜色进度条设置
利用PS绘制颜色进度条文件,保存为jpg格式,等,然后加入资源文件,利用QCombobox作为ICON添加进入,代码部分如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 QPixmap icon1(":/colorBar/colorBar/1.jpg"); 2 QPixmap icon2(":/colorBar/colorBar/2.jpg"); 3 QPixmap icon3(":/colorBar/colorBar/3.jpg"); 4 QPixmap icon4(":/colorBar/colorBar/4.jpg"); 5 QPixmap icon5(":/colorBar/colorBar/5.jpg"); 6 7 ui->colorComboBox->addItem(QIcon(icon1),""); 8 ui->colorComboBox->addItem(QIcon(icon2),""); 9 ui->colorComboBox->addItem(QIcon(icon3),""); 10 ui->colorComboBox->addItem(QIcon(icon4),""); 11 ui->colorComboBox->addItem(QIcon(icon5),"");
四、利用QGraphicsView显示openCV的Mat图像
具体代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 bool classifiedMapView::showClassifiedMap(cv::Mat img) 2 { 3 if(img.channels()!=3) 4 return 0; 5 if(!img.isContinuous()) 6 return 0; 7 8 cv::cvtColor(img,img,CV_BGR2RGB); 9 qDebug()<<img.channels()<<img.type(); 10 QImage image = QImage((const unsigned char*)(img.data),img.cols,img.rows,img.cols*img.channels(), 11 QImage::Format_RGB888); 12 QGraphicsScene *tmpScene = new QGraphicsScene(this); 13 QGraphicsPixmapItem *imgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image)); 14 tmpScene->addItem(imgItem); 15 this->setScene(tmpScene); 16 return 1; 17 18 }
五、总体效果
放松不放纵--忙碌不盲目