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 小时前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
桥田智能2 小时前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
один but you3 小时前
constexpr函数
c++
凡人叶枫3 小时前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫3 小时前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss4 小时前
BRpc使用
c++·rpc
-森屿安年-4 小时前
63. 不同路径 II
c++·算法·动态规划
chase_my_dream4 小时前
Cartographer详细讲解
c++·人工智能·自动驾驶
森G4 小时前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
碧海蓝天20224 小时前
C++法则24:在标准 C++ 中,没有任何可移植的方式判断指针 T* pt 指向的内存位置是否已经 构造了对象,程序员必须手动跟踪哪些元素已构造。
java·开发语言·c++