QT设置窗口边框圆角及样式失效

1.  直接设置样式    setStyleSheet("border:5px solid red;border-radius:10px")

2.  this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明

  this->setWindowFlags(Qt::FramelessWindowHint); //设置无边框窗口 

void SystemWarnDialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);	// 反锯齿;
    painter.setBrush(QBrush(Qt::gray));
    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 15, 15);//也可用QPainterPath 绘制代替 
    // painter.drawRoundedRect(rect, 15, 15);
    // {
    //     QPainterPath painterPath;
    //     painterPath.addRoundedRect(rect, 15, 15);
    //     p.drawPath(painterPath);
    // }
    QWidget::paintEvent(event);
}

3.  主窗体加载不了样式时需要在paintEvent事件中加上如下代码:

{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(event);
}

  

posted @ 2023-05-09 11:25  徐沛东  阅读(663)  评论(0编辑  收藏  举报