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

相关推荐
FMRbpm3 分钟前
栈练习--------从链表中移除节点(LeetCode 2487)
数据结构·c++·leetcode·链表·新手入门
编程小Y14 分钟前
C++ 静态库与动态库
c++
不秃头的帅哥22 分钟前
程序地址空间(基于c++和linxu的一些个人笔记
linux·开发语言·c++·操作系统·内存空间
Tandy12356_24 分钟前
手写TCP/IP协议栈——无回报ARP包生成
c语言·c++·tcp/ip·计算机网络
四维碎片37 分钟前
【Qt】QTimer 学习笔记总结
笔记·qt·学习
mjhcsp1 小时前
C++ 结构体(struct):自定义数据类型的核心解析
c++·结构体
ULTRA??1 小时前
C++类型和容器在MoonBit中的对应关系整理
开发语言·c++·rust
李白同学1 小时前
C++:queue、priority_queue的使用和模拟实现
开发语言·c++
楼田莉子1 小时前
Linux学习:基础IO相关学习
linux·开发语言·c++·后端·学习
.小小陈.1 小时前
C++初阶5:string类使用攻略
开发语言·c++·学习·算法