14
14
DWIDGET_USE_NAMESPACE
15
15
16
16
const int ITEM_HEIGHT = 64 ;
17
+ const int ITEM_SPACE = 10 ;
18
+ const int LABEL_SPACE = 2 ;
17
19
const int RADIUS_VALUE = 8 ;
18
20
const QSize IMAGE_SIZE = QSize(48 , 48 );
19
21
@@ -30,6 +32,8 @@ void UserItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
30
32
31
33
QPen pen;
32
34
QRect rect = option.rect ;
35
+ // item间要留10空间的空白
36
+ rect.setHeight (rect.height () - ITEM_SPACE);
33
37
34
38
if (option.state .testFlag (QStyle::State_Selected)) {
35
39
// 鼠标悬停背景色
@@ -77,7 +81,16 @@ void UserItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
77
81
QSize UserItemDelegate::sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
78
82
{
79
83
Q_UNUSED (index)
80
- return QSize (option.rect .width (), ITEM_HEIGHT);
84
+
85
+ UserItemData userData = index.data (StaticUserDataRole).value <UserItemData>();
86
+ int height = ITEM_SPACE + displayNameHeight () + LABEL_SPACE + userTypeHeight () + ITEM_SPACE;
87
+ if (!userData.name .isEmpty ()) {
88
+ height += nameHeight () + LABEL_SPACE;
89
+ }
90
+
91
+ height = height > ITEM_HEIGHT ? height : ITEM_HEIGHT;
92
+
93
+ return QSize (option.rect .width (), height);
81
94
}
82
95
83
96
void UserItemDelegate::drawRoundImage (QPainter *thisPainter, const QRect &rect, const QString &path) const
@@ -86,10 +99,14 @@ void UserItemDelegate::drawRoundImage(QPainter *thisPainter, const QRect &rect,
86
99
return ;
87
100
88
101
// 设计图上常量
89
- const int margTop = 8 ;
102
+ int margTop = 0 ;
90
103
const int marginLeft = 8 ;
91
104
const int imageRadius = 10 ;
92
105
106
+ if (rect.height () > IMAGE_SIZE.height ()) {
107
+ margTop = rect.height () / 2 - IMAGE_SIZE.height () / 2 ;
108
+ }
109
+
93
110
QRect drawRect = QRect (rect.left () + marginLeft, rect.top () + margTop,
94
111
IMAGE_SIZE.width (), IMAGE_SIZE.height ());
95
112
QPainterPath clipPath;
@@ -124,24 +141,24 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
124
141
// 文字区域设计图常量
125
142
const int leftMargin = 65 ; // 开始位置距离item最左边
126
143
const int rightMargin = 48 ; // 结束位置距离item最右边
127
- const int topMargin = 6 ;
128
- const int displayNameAreaHeight = 29 ;
144
+ const int topMargin = 0 ;
145
+
129
146
const int itemSpacing = 10 ;
130
- const int nameAreaHeight = 20 ;
131
- const int typeAreaHeight = 17 ;
132
- const int vItemSpacing = 2 ;
133
147
134
148
int textAreaWidth = rect.width () - leftMargin - rightMargin;
135
149
QRect displayNameRect = QRect (rect.left () + leftMargin, rect.top () + topMargin,
136
- textAreaWidth, displayNameAreaHeight );
150
+ textAreaWidth, displayNameHeight () );
137
151
138
152
QFont font = DFontSizeManager::instance ()->t4 ();
139
153
font.setBold (true );
140
154
painter->setFont (font);
141
155
142
156
// 绘制displayName, +1个像素是为了避免刚好width和文本宽度一致,最后文本还有省略号
143
157
QString displayNameText = elidedText (userData.displayName , textAreaWidth + 1 , font.pixelSize (), true );
144
- painter->drawText (displayNameRect, displayNameText);
158
+
159
+ QTextOption qTextOption;
160
+ qTextOption.setAlignment (Qt::AlignmentFlag::AlignVCenter);
161
+ painter->drawText (displayNameRect, displayNameText, qTextOption);
145
162
146
163
QFont typeFont = DFontSizeManager::instance ()->t8 ();
147
164
int userTypeAreaWidth = stringWidth (userData.userStrType , typeFont.pixelSize ());
@@ -155,8 +172,8 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
155
172
156
173
QString nameText = elidedText (userData.name , textAreaWidth - itemSpacing - userTypeAreaWidth, font.pixelSize ());
157
174
nameWidth = stringWidth (nameText, font.pixelSize ());
158
- QRect nameRect = QRect (displayNameRect.left (), displayNameRect.bottom () + vItemSpacing ,
159
- textAreaWidth - itemSpacing - userTypeAreaWidth, nameAreaHeight );
175
+ QRect nameRect = QRect (displayNameRect.left (), displayNameRect.bottom () + LABEL_SPACE ,
176
+ textAreaWidth - itemSpacing - userTypeAreaWidth, nameHeight () );
160
177
161
178
painter->drawText (nameRect, nameText);
162
179
}
@@ -166,8 +183,11 @@ void UserItemDelegate::drawNameAndType(QPainter *painter, const UserItemData &us
166
183
167
184
// 绘制userType, +1个像素为了避免字体大小不一样,绘制中心位置不一致
168
185
int userTypeLeft = userData.name .isEmpty () ? displayNameRect.left () : displayNameRect.left () + nameWidth + itemSpacing;
169
- QRect userTypeRect = QRect (userTypeLeft, displayNameRect.bottom () + vItemSpacing + 1 , userTypeAreaWidth + 1 , typeAreaHeight);
170
- painter->drawText (userTypeRect, userData.userStrType );
186
+ QRect userTypeRect = QRect (userTypeLeft, displayNameRect.bottom () + LABEL_SPACE + 1 , userTypeAreaWidth + 1 , userTypeHeight ());
187
+ qreal currentOpacity = painter->opacity ();
188
+ painter->setOpacity (0.7 );
189
+ painter->drawText (userTypeRect, userData.userStrType , qTextOption);
190
+ painter->setOpacity (currentOpacity);
171
191
}
172
192
173
193
void UserItemDelegate::drawCheckedState (QPainter *painter, const QRect &rect) const
@@ -218,3 +238,24 @@ QString UserItemDelegate::elidedText(const QString &originString, int width, int
218
238
QFontMetrics fm (font);
219
239
return fm.elidedText (originString, Qt::ElideRight, width);
220
240
}
241
+
242
+ int UserItemDelegate::displayNameHeight ()
243
+ {
244
+ QFont font = DFontSizeManager::instance ()->t4 ();
245
+ QFontMetrics fm (font);
246
+ return fm.height ();
247
+ }
248
+
249
+ int UserItemDelegate::nameHeight ()
250
+ {
251
+ QFont font = DFontSizeManager::instance ()->t6 ();
252
+ QFontMetrics fm (font);
253
+ return fm.height ();
254
+ }
255
+
256
+ int UserItemDelegate::userTypeHeight ()
257
+ {
258
+ QFont font = DFontSizeManager::instance ()->t8 ();
259
+ QFontMetrics fm (font);
260
+ return fm.height ();
261
+ }
0 commit comments