-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomtreeviewdelegate.cpp.autosave
More file actions
74 lines (67 loc) · 2.69 KB
/
customtreeviewdelegate.cpp.autosave
File metadata and controls
74 lines (67 loc) · 2.69 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "customtreeviewdelegate.h"
#include <QPainter>
#include <QDebug>
#include <QStyle>
#include <QApplication>
#include <QStandardItemModel>
customtreeViewDelegate::customtreeViewDelegate(QObject *parent):
QStyledItemDelegate(parent)
{
}
void customtreeViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
const QStandardItemModel *model = qobject_cast<const QStandardItemModel *>(index.model());
if(!index.parent().isValid()) //mainItem
{
//绘制mainItem的Text
painter->setPen(QColor(Qt::black));
painter->setFont(QFont("Times", 9, QFont::Bold));
painter->drawText(option.rect.x()+5,option.rect.y(),option.rect.width(),option.rect.height(), Qt::AlignLeft|Qt::AlignVCenter, index.data().toString());
//绘制mianItem的checkbox
auto data = model->data(index, Qt::CheckStateRole);
QStyleOptionButton checkBoxStyle;
if(data == Qt::Unchecked)
{
checkBoxStyle.state |= QStyle::State_Off;
}
else if(data == Qt::PartiallyChecked)
{
checkBoxStyle.state |= QStyle::State_NoChange;
}
else if(data == Qt::Checked)
{
checkBoxStyle.state |= QStyle::State_On;
}
checkBoxStyle.state |= QStyle::State_Enabled;
checkBoxStyle.iconSize = QSize(20, 20);
checkBoxStyle.rect = QRect(QPoint(option.rect.right()-30,option.rect.top()+5),
QPoint(option.rect.right()-10,option.rect.bottom()-5));
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter);
}else
{
//鼠标悬浮在Item上或者选中Item
if( (option.state & QStyle::State_MouseOver || model->itemFromIndex(index)->checkState() == Qt::Checked) && (option.state & QStyle::State_Enabled))
{
painter->setBrush(QColor("#28163d"));
}else
{
painter->setBrush(QColor("#a5a5a5"));
}
painter->setPen(Qt::NoPen);
painter->drawRect(QRect(QPoint(2,option.rect.top()+4),
QPoint(option.rect.right()-2,option.rect.bottom()-4)));
painter->setFont(QFont("SimSun", 9));
painter->setPen(QColor("#ffffff"));
painter->drawText(option.rect, Qt::AlignCenter, index.data().toString());
}
painter->restore();
}
//指示主Item项和childItem项的高度
QSize customtreeViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.parent().isValid())
return QSize(option.rect.width(), 30);
else
return QSize(option.rect.width(), 40);
}