@@ -65,6 +65,41 @@ class QAction;
65
65
class QMenu ;
66
66
QT_END_NAMESPACE
67
67
68
+
69
+ #include < QItemDelegate>
70
+
71
+ class Delegate : public QItemDelegate
72
+ {
73
+ public:
74
+ QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
75
+ {
76
+ QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
77
+ QSize iconsize = icon.actualSize (option.decorationSize );
78
+ QRect item = option.rect ;
79
+ QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 ), item.width ()-10 , item.height ()-10 );
80
+ QSize txtsize = option.fontMetrics .boundingRect (txtRect, Qt::AlignCenter|Qt::TextWrapAnywhere, index .data ().toString ()).size ();
81
+ return QSize (txtsize.width (),txtsize.height ()+iconsize.height ());
82
+ }
83
+ void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
84
+ {
85
+ QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
86
+ QSize iconsize = icon.actualSize (option.decorationSize );
87
+ QRect item = option.rect ;
88
+ QRect iconRect (item.left ()+(item.width ()/2 )-(iconsize.width ()/2 ), item.top ()+3 , iconsize.width (), iconsize.height ());
89
+ QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 )+5 , item.width (), item.height ());
90
+
91
+ if (option.state & QStyle::State_Selected) {
92
+ painter->fillRect (option.rect , option.palette .highlight ());
93
+ painter->setPen (option.palette .highlightedText ().color ());
94
+ } else {
95
+ painter->setPen (option.palette .text ().color ());
96
+ }
97
+
98
+ painter->drawPixmap (iconRect, icon.pixmap (iconsize.width (),iconsize.height ()));
99
+ painter->drawText (txtRect, Qt::AlignCenter|Qt::TextWrapAnywhere, index .data ().toString ());
100
+ }
101
+ };
102
+
68
103
// ---------------------------------------------------------------------------------
69
104
70
105
/* *
0 commit comments