Qt custom delegation draws control, picture and text in QTableView

Keywords: Qt

Original address: http://blog.csdn.net/zhi379/article/details/28412189


Qt custom delegation draws control, picture and text in QTableView


Original link: http://qimo601.iteye.com/blog/1539147
    Blog category:
  • Qt
QtCustom delegationQTableViewQCheckBoxprogress bar 

Custom delegate, inherited from, qstyleditemtdelegate class, overloaded Paint() function,

1. Implementation of drawing format string in QTableView

2. Drawing progress bar in QTableView

3. Drawing QCheckBox in QTableView

4. Drawing stars in QTableView

5. Drawing Pixmap picture in QTableView

 

 

 

1. Implementation of drawing format string in QTableView

 

  1. //Overloaded drawing function  
  2. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,  
  3.                           const QModelIndex &index) const  
  4. {  
  5.     //If column 2 'patient Id'  
  6.     if (index.column() == 2)  
  7.     {  
  8.         //Get current item value  
  9.         int patientId = index.model()->data(index, Qt::DisplayRole).toInt();  
  10.         //Set 'patient Id' format string: P: 00000 X; 6-digit decimal number, 0 if not enough;  
  11.         QString text = QString("P:%1").arg(patientId, 6, 10, QChar('0'));  
  12.   
  13.   
  14.         //Get item style settings  
  15.         QStyleOptionViewItem myOption = option;  
  16.         myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;  
  17.     //Draw text  
  18.     QApplication::style()->drawItemText ( painter, myOption.rect , myOption.displayAlignment, QApplication::palette(), true,text );  
  19.           
  20.           
  21.     }  
  22. else  
  23.     {  
  24.         //Otherwise, call the default delegate  
  25.         QStyledItemDelegate::paint(painter, option, index);  
  26.     }  
  27.       
  28. }  

 

If the custom delegate inherits from the QItemDelegate class and draws a string, you can directly use the drawDisplay() in QItemDelegate;

 

  1. void TrackDelegate::paint(QPainter *painter,    
  2.                           const QStyleOptionViewItem &option,    
  3.                           const QModelIndex &index) const    
  4. {    
  5.     //Save track columns  
  6.     if (index.column() == durationColumn) {    
  7.         //Get the index corresponding to the data in the Model  
  8.         int secs = index.model()->data(index, Qt::DisplayRole).toInt();    
  9.         //Format time string minutes: seconds  
  10.         QString text = QString("%1:%2")    
  11.                        .arg(secs / 60, 2, 10, QChar('0'))    
  12.                        .arg(secs % 60, 2, 10, QChar('0'));    
  13.         //Get item style settings  
  14.         QStyleOptionViewItem myOption = option;    
  15.         myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;    
  16.         //Draw text  
  17.         drawDisplay(painter, myOption, myOption.rect, text);    
  18.         //If there is currently focus, draw a focus rectangle, otherwise do nothing  
  19.         drawFocus(painter, myOption, myOption.rect);    
  20.     } else{    
  21.         //Otherwise default  
  22.         QItemDelegate::paint(painter, option, index);    
  23.     }    
  24. }    
  

2. Drawing progress bar in QTableView

  1. //Overloaded drawing function  
  2. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,  
  3.                           const QModelIndex &index) const  
  4. {  
  5.     //If 'number of subtasks completed'  
  6.      if (index.column() == 9)  
  7.     {  
  8.         const QAbstractItemModel *itemModel = index.model();  
  9.         //Get the index corresponding to the data in the Model  
  10.         int finishedSubTaskNum = itemModel->data(index, Qt::DisplayRole).toInt();  
  11.         int subTaskNum = itemModel->data(itemModel->index(index.row(),8), Qt::DisplayRole).toInt();  
  12.   
  13.         //Style options for progress bar  
  14.         QStyleOptionProgressBarV2 *progressBarOption = new QStyleOptionProgressBarV2();  
  15.         progressBarOption->rect = option.rect;  
  16.         progressBarOption->minimum = 0;  
  17.         progressBarOption->maximum = subTaskNum;  
  18.         progressBarOption->progress = finishedSubTaskNum;  
  19.         int t = finishedSubTaskNum/subTaskNum;  
  20.         progressBarOption->text = QString::number(t) + "%";  
  21.         progressBarOption->textVisible = true;  
  22.         //Draw progress bar  
  23.         QApplication::style()->drawControl(QStyle::CE_ProgressBar, progressBarOption, painter);  
  24.   
  25.     }  
  26.    else  
  27.     {  
  28.         //Otherwise, call the default delegate  
  29.         QStyledItemDelegate::paint(painter, option, index);  
  30.     }  
  31.       
  32. }  

3. Drawing QCheckBox in QTableView

 

 

  1. #include <QtGui>  
  2. #include <QItemDelegate>  
  3. #include <QStyleOptionProgressBarV2>  
  4. #include "DelReconQueue.h"  
  5. //Overloaded drawing function  
  6. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,  
  7.                           const QModelIndex &index) const  
  8. {  
  9.      if (index.column() == 11)  
  10.     {  
  11.         //Get value  
  12.         bool checked = index.model()->data(index, Qt::DisplayRole).toBool();  
  13.         //Style options for buttons  
  14.         QStyleOptionButton *checkBoxOption = new QStyleOptionButton();  
  15.         checkBoxOption->state |= QStyle::State_Enabled;  
  16.         //Judge whether to select according to the value  
  17.         if(checked)  
  18.         {  
  19.             checkBoxOption->state |= QStyle::State_On;  
  20.         }  
  21.         else  
  22.         {  
  23.             checkBoxOption->state |= QStyle::State_Off;  
  24.         }  
  25.         //Return to QCheckBox geometry  
  26.         checkBoxOption->rect = CheckBoxRect(option);  
  27.         //Draw QCheckBox  
  28.         QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter);  
  29.   
  30.     }  
  31.   
  32.     else  
  33.     {  
  34.         //Otherwise, call the default delegate  
  35.         QStyledItemDelegate::paint(painter, option, index);  
  36.     }  
  37.       
  38. }  

/ / generate QCheckBox

  1. QRect DgSystemLog::CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)const  
  2. {  
  3.     //Parameters required for drawing buttons  
  4.     QStyleOptionButton checkBoxStyleOption;  
  5.     //Returns the element sub region according to the given style parameter  
  6.     QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);  
  7.     //Return QCheckBox coordinates  
  8.     QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,  
  9.                          viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);  
  10.     //Return to QCheckBox geometry  
  11.     return QRect(checkBoxPoint, checkBoxRect.size());  
  12. }  
 

 

4. Implementation of drawing custom stars in QTableView

 

  1. //Overloaded drawing function  
  2. void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,  
  3.                          const QModelIndex &index) const  
  4. {  
  5.     //If a data is a star type  
  6.     if (qVariantCanConvert<StarRating>(index.data())) {  
  7.         //Get the data and convert it to StarRating type  
  8.         StarRating starRating = qVariantValue<StarRating>(index.data());  
  9.         //If a control is selected, we will make the selected control bright  
  10.         if (option.state & QStyle::State_Selected)  
  11.             painter->fillRect(option.rect, option.palette.highlight());  
  12.   
  13.         starRating.paint(painter, option.rect, option.palette,  
  14.                          StarRating::ReadOnly);  
  15.     }  
  16.     //If no control is selected, call the default delegate  
  17.     else {  
  18.         QStyledItemDelegate::paint(painter, option, index);  
  19.     }  
  20. }  
 

Star custom source file

 

  1. #ifndef STARRATING_H  
  2. #define STARRATING_H  
  3.   
  4. #include <QMetaType>  
  5. #include <QPointF>  
  6. #include <QVector>  
  7.   
  8. class StarRating  
  9. {  
  10. public:  
  11.     enum EditMode { Editable, ReadOnly };  
  12.   
  13.     StarRating(int starCount = 1, int maxStarCount = 5);  
  14.   
  15.     void paint(QPainter *painter, const QRect &rect,  
  16.                const QPalette &palette, EditMode mode) const;  
  17.     QSize sizeHint() const;  
  18.     int starCount() const { return myStarCount; }  
  19.     int maxStarCount() const { return myMaxStarCount; }  
  20.     void setStarCount(int starCount) { myStarCount = starCount; }  
  21.     void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; }  
  22.   
  23. private:  
  24.     QPolygonF starPolygon;  
  25.     QPolygonF diamondPolygon;  
  26.     int myStarCount;  
  27.     int myMaxStarCount;  
  28. };  
  29.   
  30. //Let all template types know about this class, including QVariant  
  31. Q_DECLARE_METATYPE(StarRating)  
  32.   
  33. #endif  
  34.   
  35.   
  36.   
  37. #include <QtGui>  
  38. #include <math.h>  
  39.   
  40. #include "starrating.h"  
  41.   
  42. const int PaintingScaleFactor = 20;  
  43.   
  44. StarRating::StarRating(int starCount, int maxStarCount)  
  45. {  
  46.     myStarCount = starCount;  
  47.     myMaxStarCount = maxStarCount;  
  48.   
  49.     starPolygon << QPointF(1.0, 0.5);  
  50.     for (int i = 1; i < 5; ++i)  
  51.         starPolygon << QPointF(0.5 + 0.5 * cos(0.8 * i * 3.14),  
  52.                                0.5 + 0.5 * sin(0.8 * i * 3.14));  
  53.   
  54.     diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4)  
  55.                    << QPointF(0.6, 0.5) << QPointF(0.5, 0.6)  
  56.                    << QPointF(0.4, 0.5);  
  57. }  
  58.   
  59. QSize StarRating::sizeHint() const  
  60. {  
  61.     return PaintingScaleFactor * QSize(myMaxStarCount, 1);  
  62. }  
  63.   
  64. void StarRating::paint(QPainter *painter, const QRect &rect,  
  65.                        const QPalette &palette, EditMode mode) const  
  66. {  
  67.     painter->save();  
  68.   
  69.     painter->setRenderHint(QPainter::Antialiasing, true);  
  70.     painter->setPen(Qt::NoPen);  
  71.   
  72.     if (mode == Editable) {  
  73.         painter->setBrush(palette.highlight());  
  74.     } else {  
  75.         painter->setBrush(palette.foreground());  
  76.     }  
  77.   
  78.     int yOffset = (rect.height() - PaintingScaleFactor) / 2;  
  79.     painter->translate(rect.x(), rect.y() + yOffset);  
  80.     //Brush coordinates  
  81.     painter->scale(PaintingScaleFactor, PaintingScaleFactor);  
  82.   
  83.     for (int i = 0; i < myMaxStarCount; ++i) {  
  84.         if (i < myStarCount) {  
  85.             painter->drawPolygon(starPolygon, Qt::WindingFill);  
  86.         } else if (mode == Editable) {  
  87.             painter->drawPolygon(diamondPolygon, Qt::WindingFill);  
  88.         }  
  89.         painter->translate(1.0, 0.0);  
  90.     }  
  91.   
  92.     painter->restore();  
  93. }  

5. Drawing Pixmap picture in QTableView ,

See specific examples for details Qt - inserts a picture into a table (QTableView)

 

  1. void MyItemDelegate::paint(QPainter * painter,    
  2.                            const QStyleOptionViewItem & option,    
  3.                            const QModelIndex & index) const    
  4. {    
  5.     if(index.column()!=0){    
  6.         QItemDelegate::paint(painter,option,index);    
  7.         return;    
  8.     }    
  9.     const QAbstractItemModel * model=index.model();    
  10.     QVariant var=model->data(index,Qt::CheckStateRole);    
  11.     if(var.isNull()) var=false;    
  12.     const QPixmap & star=var.toBool()?    
  13. favouritePixmap:notFavouritePixmap;    
  14.     
  15.     int width=star.width();    
  16.     int height=star.height();    
  17.     QRect rect=option.rect;    
  18.     int x=rect.x()+rect.width()/2-width/2;    
  19.     int y=rect.y()+rect.height()/2-height/2;    
  20.     
  21.     painter->drawPixmap(x,y,star);    
  22. }    
 



Posted by purpendicular on Thu, 30 Apr 2020 13:38:27 -0700