-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprogressbardelegate.cpp
57 lines (48 loc) · 1.93 KB
/
progressbardelegate.cpp
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
#include "progressbardelegate.h"
#include <QPainter>
#include <QtWidgets>
ProgressBarDelegate::ProgressBarDelegate(QObject *parent)
: QStyledItemDelegate(parent)
, m_progressBarPtr(new QProgressBar)
{
m_progressBarPtr->setOrientation(Qt::Horizontal);
}
ProgressBarDelegate::~ProgressBarDelegate() {}
void ProgressBarDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption, index);
if (option.state.testFlag(QStyle::State_HasFocus)) {
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, viewOption, index);
int value = index.model()->data(index).toUInt();
if (value < 0) {
value = 0;
} else if (value > 100) {
value = 100;
}
int w = qMin(option.rect.width(), option.rect.height()) / 10.0;
QStyleOptionProgressBar progressBarOption;
progressBarOption.initFrom(option.widget);
progressBarOption.rect = option.rect.adjusted(w, w, -w, -w);
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
progressBarOption.progress = value;
progressBarOption.text = tr("%1%").arg(progressBarOption.progress);
progressBarOption.state = QStyle::StateFlag::State_Horizontal;
painter->save();
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
painter->setBrush(option.palette.highlightedText());
}
option.widget->style()->drawControl(QStyle::CE_ProgressBar,
&progressBarOption,
painter,
m_progressBarPtr.data());
painter->restore();
}