#include "linelabel.h" #include #include LineLabel::LineLabel(QWidget* parent /*= nullptr*/, QColor color /*= QColor(0xCC, 0xCC, 0xCC)*/, QSize size /*= QSize(150, 24)*/) : QLabel(parent), m_color(color) { setFixedSize(size); } LineLabel::~LineLabel() {} void LineLabel::paintEvent(QPaintEvent *event) { Q_UNUSED(event) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // 如果使用的是默认的灰色,则转换为调色板中的按钮背景色 if (m_color == QColor(0xCC, 0xCC, 0xCC)) { QColor background = this->palette().color(QPalette::Button); painter.setPen(background); } else { painter.setPen(m_color); } QRect rect = this->rect(); painter.drawLine(0, rect.height()/2, rect.width(), rect.height()/2); QLabel::paintEvent(event); }