数字提示(圆角矩形)

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
void Label::paintEvent(QPaintEvent *e)
{
QPainter painter(this);

const qreal radius = 10;
QRectF rect = QRect(10, 200, 20, 20);

QPainterPath path;
path.moveTo(rect.bottomRight() - QPointF(0, radius));
path.lineTo(rect.topRight() + QPointF(0, radius));
path.arcTo(QRectF(QPointF(rect.topRight() - QPointF(radius * 2, 0)), QSize(radius * 2, radius *2)), 0, 90);
path.lineTo(rect.topLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rect.topLeft()), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rect.bottomLeft() - QPointF(0, radius));
path.arcTo(QRectF(QPointF(rect.bottomLeft() - QPointF(0, radius * 2)), QSize(radius * 2, radius * 2)), 180, 90);
path.lineTo(rect.bottomLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rect.bottomRight() - QPointF(radius * 2, radius * 2)), QSize(radius * 2, radius * 2)), 270, 90);

painter.fillPath(path, QColor(Qt::red));

QPen pen = painter.pen();
pen.setColor(Qt::white);
painter.setPen(pen);
painter.drawText(rect, Qt::AlignCenter, QString::number(99));

{
painter.save();
const qreal radius = 10;
QRectF rect = QRect(50, 200, 50, 20);

QPainterPath path;

path.moveTo(rect.bottomRight() - QPointF(0, radius));
path.lineTo(rect.topRight() + QPointF(0, radius));
path.arcTo(QRectF(QPointF(rect.topRight() - QPointF(radius * 2, 0)), QSize(radius * 2, radius *2)), 0, 90);
path.lineTo(rect.topLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rect.topLeft()), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rect.bottomLeft() - QPointF(0, radius));
path.arcTo(QRectF(QPointF(rect.bottomLeft() - QPointF(0, radius * 2)), QSize(radius * 2, radius * 2)), 180, 90);
path.lineTo(rect.bottomLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rect.bottomRight() - QPointF(radius * 2, radius * 2)), QSize(radius * 2, radius * 2)), 270, 90);

painter.fillPath(path, QColor(Qt::red));

QPen pen = painter.pen();
pen.setColor(Qt::white);
painter.setPen(pen);
painter.drawText(rect, Qt::AlignCenter, QString::number(9999999));
painter.restore();
}
}

QtExamples-NumTip

文本后面添加数字提示

需求:树形结构的每一项的文本后面添加数字提示

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
// paint
int leftMargin = 2;
int widthNumTip = 18;
int heightNumTip = 16;
int xNumTip = r.width();

int num = m_manager ? m_manager->getNumTip(index) : getNumTip(index);
auto numTipVisible = m_manager ? m_manager->isNumTipVisible(num) : isNumTipVisible(num);

if (numTipVisible) {
// 根据数字的位数计算圆角矩形的宽度
widthNumTip += 3 * QString::number(num).length();

xNumTip = r.width() - widthNumTip;
}

QFontMetrics fm = painter->fontMetrics();
flags = Qt::AlignLeft | Qt::AlignVCenter;

auto textWidth = fm.width(text);
if (textWidth < xNumTip) {
xNumTip = textWidth;
} else {
text = fm.elidedText(text, Qt::ElideRight, numTipVisible ? r.width() - widthNumTip : r.width(), Qt::TextShowMnemonic);
}
painter->drawText(r, flags, text);

if (numTipVisible) {
const qreal radius = heightNumTip / 2;
QRectF rectNumTip = QRect(r.left() + xNumTip + leftMargin, newOption.rect.top() + (newOption.rect.height() - heightNumTip) / 2,
widthNumTip, heightNumTip);

QPainterPath path;
path.moveTo(rectNumTip.bottomRight() - QPointF(0, radius));
path.lineTo(rectNumTip.topRight() + QPointF(0, radius));
path.arcTo(QRectF(QPointF(rectNumTip.topRight() - QPointF(radius * 2, 0)), QSize(radius * 2, radius *2)), 0, 90);
path.lineTo(rectNumTip.topLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rectNumTip.topLeft()), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rectNumTip.bottomLeft() - QPointF(0, radius));
path.arcTo(QRectF(QPointF(rectNumTip.bottomLeft() - QPointF(0, radius * 2)), QSize(radius * 2, radius * 2)), 180, 90);
path.lineTo(rectNumTip.bottomLeft() + QPointF(radius, 0));
path.arcTo(QRectF(QPointF(rectNumTip.bottomRight() - QPointF(radius * 2, radius * 2)), QSize(radius * 2, radius * 2)), 270, 90);
painter->setRenderHint(QPainter::Antialiasing);
painter->fillPath(path, QColor(Qt::red));

painter->save();
QPen pen = painter->pen();
pen.setWidth(2);
pen.setColor(Qt::white);
painter->setPen(pen);
painter->drawText(rectNumTip, Qt::AlignCenter, QString::number(num));
painter->restore();
}

QtExamples-NumTipNearText