void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 设置反锯齿;
// 通过MouseEnterEvent() & MouseLeaveEvent()函数检查是否鼠标在此Widget上
if (!isHovered)
{
// 如鼠标进去此Widget时,边框改变颜色
const QColor color(50, 50, 63);
painter.setPen(color);
}
else
{
// 如鼠标离开此Widget时,边框改变颜色
const QColor color(86, 85, 96);
painter.setPen(color);
}
painter.setBrush(QBrush(Qt::transparent)); // 设置填充颜色为透明
QRect rect = this->rect();
rect.setWidth(rect.width() - 1);
rect.setHeight(rect.height() - 1);
painter.drawRoundedRect(rect, 8, 8); // 绘制带圆角的矩形
QWidget::paintEvent(event);
}
参考:(https://blog.csdn.net/GoForwardToStep/article/details/52084538)