33 lines
1.0 KiB
C++
33 lines
1.0 KiB
C++
#include "circlelabel.h"
|
|
#include <QPainter>
|
|
#include <QBrush>
|
|
#include <QPalette>
|
|
|
|
CircleLable::CircleLable(const QString& text, QWidget* parent /*= nullptr*/, int size /*= 24*/, QColor backgroundColor /*= QColor(0xCC, 0xCC, 0xCC)*/) :
|
|
m_text(text),
|
|
m_backgroundColor(backgroundColor),
|
|
QLabel(parent)
|
|
{
|
|
setFixedSize(QSize(size, size));
|
|
}
|
|
|
|
CircleLable::~CircleLable()
|
|
{}
|
|
|
|
void CircleLable::paintEvent(QPaintEvent *event)
|
|
{
|
|
QPainter painter(this);
|
|
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
|
|
painter.setBrush(QBrush(m_backgroundColor));
|
|
painter.setPen(Qt::white);
|
|
QRect rect = this->rect();
|
|
// 也可用QPainterPath 绘制代替 painter.drawRoundedRect(rect, 15, 15); { QPainterPath painterPath; painterPath.addRoundedRect(rect, 15, 15); p.drawPath(painterPath); }
|
|
painter.drawRoundedRect(rect, rect.width()/2, rect.width()/2);
|
|
|
|
// drawText
|
|
rect.setHeight(rect.height() - 2);
|
|
painter.drawText(rect, Qt::AlignCenter, m_text);
|
|
|
|
QLabel::paintEvent(event);
|
|
}
|