Qt自定义一个圆角对话框

如何得到一个圆角对话框?

步骤:

1、继承自QDiaglog

2、去掉系统自带的边框

3、设置背景透明,不设置4个角会有多余的部分出现颜色

4、对话框内部添加1个QWidget,给这个widget设置圆角,并添加到布局中让他充满对话框

5、后续对话框的所有内容都添加在这个widget里面

6、模拟QMessageBox的静态方法,提供一个静态方法,调用这个静态方法可以直接显示一个圆角对话框

举例:

cpp 复制代码
#ifndef ROUNDEDDIALOG_H
#define ROUNDEDDIALOG_H

#include <QDialog>
#include<QHBoxLayout>
#include<QLabel>
#include<QPushButton>

class RoundedDialog : public QDialog
{
    Q_OBJECT

public:
    //模拟QMessageBox的静态方法,调用这个静态方法可以直接显示一个圆角对话框
    static int roundedDialog()
    {
        RoundedDialog d;
        return d.exec();
    }


    RoundedDialog(QWidget *parent = nullptr) : QDialog(parent)
    {
        resize(400,200);
        //1.去掉系统自带的边框
        setWindowFlag(Qt::FramelessWindowHint);
        //2.设置背景透明,不设置4个角会有颜色
        setAttribute(Qt::WA_TranslucentBackground);

        //内部添加1个QWidget,给这个widget设置圆角,并添加到布局中让他充满对话框
        QHBoxLayout* h_box=new QHBoxLayout(this);
        h_box->setSpacing(0);
        h_box->setContentsMargins(0,0,0,0);

        QWidget* w=new QWidget(this);
        w->setStyleSheet(".QWidget{border-radius:20px;background-color:green}");
        h_box->addWidget(w);

        //后续对话框的所有内容都添加在这个widget里面
        QLabel* label=new QLabel("你好,我要说拜拜啦!",w);
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet(R"(font: 900 12pt "Arial Black";)");
        label->move(120,50);

        QPushButton* btn_close=new QPushButton("×",this);
        btn_close->setStyleSheet("border-radius:15px;font-size:18px;font-weight:bold;background-color:pink");
        btn_close->setGeometry(185,150,30,30);
        connect(btn_close,&QPushButton::clicked,this,&QDialog::accept);

    }
    ~RoundedDialog()=default;
};
#endif // ROUNDEDDIALOG_H

学习链接:https://github.com/0voice

相关推荐
·薯条大王2 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
hansang_IR7 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘7 小时前
day7.c++继承
c++
沫璃染墨8 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌8 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
醉城夜风~8 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
≮傷£≯√9 小时前
Opencv VideoCapture
qt·opencv
烟花落o9 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门
liulilittle9 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光9 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针