-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombo_box_item_delegate.cpp
More file actions
42 lines (34 loc) · 1.29 KB
/
combo_box_item_delegate.cpp
File metadata and controls
42 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Created by Administrator on 2025/5/5.
//
#include "combo_box_item_delegate.hpp"
#include <QPainter>
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent)
: QStyledItemDelegate(parent) {}
QSize ComboBoxItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const {
QSize size = QStyledItemDelegate::sizeHint(option, index);
size.setHeight(30);
return size;
}
void ComboBoxItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
// 定义 padding 值
constexpr int left = 4;
constexpr int right = 8;
constexpr int top = 4;
constexpr int bottom = 4;
// 计算绘制文本的矩形区域,padding
const QRect textRect = option.rect.adjusted(left, top, -right, -bottom);
// 绘制背景
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
} else if (option.state & QStyle::State_MouseOver) {
painter->fillRect(option.rect, QColor(240, 240, 240));
}
// 获取显示文本
const QString text = index.data(Qt::DisplayRole).toString();
// 绘制文本
painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text);
}