QT软件开发: QWidget窗口绘制圆角

2022-01-12 10:33:35 浏览数 (1)

一、运行效果

二、核心代码

代码语言:javascript复制
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
protected:
    void paintEvent(QPaintEvent *p);
    .................
}


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //隐藏标题栏
    setWindowFlags(Qt::FramelessWindowHint);//无边框 置顶

    //设置窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    //设置样式
    this->setStyleSheet("#Widget{background-color: rgba(255, 0, 0, 150);}");
}


void Widget::paintEvent(QPaintEvent *p1)
{
    //绘制样式
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式

    QBitmap bmp(this->size());
    bmp.fill();
    QPainter painter(&bmp);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawRoundedRect(bmp.rect(), 12, 12);
    setMask(bmp);
}

0 人点赞