首页 > 其他分享 >Qt 漂亮的Material样式对话框(QtMaterialDialog)

Qt 漂亮的Material样式对话框(QtMaterialDialog)

时间:2024-05-27 13:56:25浏览次数:14  
标签:QtMaterialDialog Qt Form 对话框 hButtonLayout NULLPTR QtMaterialFlatButton cancleBu

前言

  在实际项目开发中,对于漂亮美观的UI界面大多数都是采用样式表来实现的,但是实际上即使是样式表很少也能实现比较带科技炫酷的风格,本篇介绍的是模仿谷歌Material风格的UI界面,基于大牛的第三方开源控件库,界面效果大致如下

正文

我对接口进行了封装,关键代码献上:
头文件:

 1 #ifndef FORM_H
 2 #define FORM_H
 3 
 4 #include <QWidget>
 5 #include <QDebug>
 6 #include <QThread>
 7 #include <QLabel>
 8 #include <QHBoxLayout>
 9 
10 namespace Ui {
11 class Form;
12 }
13 
14 class QtMaterialRaisedButton;
15 class QtMaterialFlatButton;
16 class QtMaterialDialog;
17 
18 class Form : public QWidget
19 {
20     Q_OBJECT
21 
22 public:
23     explicit Form(QWidget *parent = 0);
24     ~Form();
25     void InitQtMaterialDialog();
26     void ShowQtMaterialDialog(const QString &text,bool haveCancle = false);
27 private slots:
28 
29 
30 
31 private:
32     Ui::Form *ui;
33     /********关于Material风格的弹出对话框需要定义的变量属性*******/
34     QtMaterialDialog * m_dialog = Q_NULLPTR;
35     QtMaterialFlatButton * yesButton = Q_NULLPTR;
36     QtMaterialFlatButton * cancleButton = Q_NULLPTR;
37     QHBoxLayout *hButtonLayout = Q_NULLPTR;
38     QLabel * label = Q_NULLPTR;
39     /********关于Material风格的弹出对话框需要定义的变量属性*******/
40 };
41 
42 #endif // FORM_H

cpp文件:
两个关键函数,初始化窗口函数以及显示窗口函数

 1 void Form::InitQtMaterialDialog()
 2 {
 3     if(m_dialog == Q_NULLPTR)
 4     {
 5         m_dialog = new QtMaterialDialog(this);
 6 
 7         QWidget *dialogWidget = new QWidget;
 8         QVBoxLayout *dialogWidgetLayout = new QVBoxLayout;
 9         dialogWidget->setLayout(dialogWidgetLayout);
10         label = new QLabel("");
11         label->setAlignment(Qt::AlignCenter);
12         dialogWidgetLayout->addWidget(label,4);
13         hButtonLayout = new QHBoxLayout;
14         yesButton = new QtMaterialFlatButton("确定"); //QtMaterialFlatButton
15         cancleButton = new QtMaterialFlatButton("取消");
16         hButtonLayout->addWidget(yesButton,1);
17         hButtonLayout->addWidget(cancleButton,1);
18         dialogWidgetLayout->addLayout(hButtonLayout,1);
19 
20         cancleButton->setMaximumWidth(150);
21         yesButton->setMaximumWidth(150);
22 
23         QVBoxLayout *dialogLayout = new QVBoxLayout;
24         m_dialog->setWindowLayout(dialogLayout);
25 
26         dialogWidget->setMinimumHeight(300);
27 
28         dialogLayout->addWidget(dialogWidget);
29         connect(cancleButton,&QtMaterialFlatButton::pressed,[this](){
30             qDebug() << "按下取消" << endl;
31             m_dialog->hideDialog();
32         });
33         connect(yesButton,&QtMaterialFlatButton::pressed,[this](){
34             qDebug() << "按下确定" << endl;
35             m_dialog->hideDialog();
36         });
37     }
38 
39 }
40 
41 void Form::ShowQtMaterialDialog(const QString &text, bool haveCancle)
42 {
43     if(m_dialog)
44     {
45         label->setText(text);
46         if(haveCancle && hButtonLayout->indexOf(cancleButton) == -1)
47         {
48             if(cancleButton == Q_NULLPTR)
49             {
50                 cancleButton = new QtMaterialFlatButton("取消");
51                 connect(cancleButton,&QtMaterialFlatButton::pressed,[this](){
52                     qDebug() << "按下取消" << endl;
53                     m_dialog->hideDialog();
54                 });
55             }
56             hButtonLayout->addWidget(cancleButton);
57             hButtonLayout->setStretch(0,1);
58             hButtonLayout->setStretch(1,1);
59         }
60         if(!haveCancle && hButtonLayout->indexOf(cancleButton) != -1)
61         {
62             hButtonLayout->removeWidget(cancleButton);
63             delete cancleButton;
64             cancleButton = Q_NULLPTR;
65         }
66         m_dialog->showDialog();
67     }
68 }

 

标签:QtMaterialDialog,Qt,Form,对话框,hButtonLayout,NULLPTR,QtMaterialFlatButton,cancleBu
From: https://www.cnblogs.com/ybqjymy/p/18215336

相关文章

  • Qt 常用类 (8)—— QIcon
    QIcon类代表图标,实现在QtGui共享库中。QIcon对象可以认为是一系列图像的组合,每个图像代表窗口在某种状态下应该1显示的图标。QIcon类支持以下构造函数:QIcon();//构造一个空图像构成的图标QIcon(constQPixmap&pixmap);//从Pixmap对象......
  • Qt/C++音视频开发75-获取本地有哪些摄像头名称/Qt内置函数方式
    一、前言在需要打开本地摄像头的场景中,有个需求绕不开,那就是如何获取本地有哪些摄像头设备名称,这样可以提供下拉框给用户选择,不然你让用户去填设备名,你觉得用户会知道是啥,他会操作吗?就算你提供了详细的查看步骤,估计也很难,如果用户是程序员还好,如果是电脑小白,鼠标都用不好,你还让他......
  • Qt QListWidget 存放自定义控件不显示问题
    问题软件功能:每点击一次新建按钮,在QListWidget新增一行自定义控件,主窗口和自定义窗口如下。主窗口:自定义窗口问题代码:Form*myform=newForm();QListWidgetItem*item=newQListWidgetItem(ui->listWidget);ui->listWidget->addItem(item);......
  • Qt基础(一)QString 用法
    QString类保存16位Unicode值,提供了丰富的操作、查询和转换等函数。该类还进行了使用隐式共享、高效的内存分配策略等多方面的优化。1.字符串拼接使用+运算符//字符串拼接//+运算符staticvoidStringConnect1(){QStringstr1="Welcome";str1=str1+......
  • Windows、Linux下,基于QT的打包方法
    整理这篇文档的意义在于:自己走了很多弯路,淋过雨所以想为别人撑伞,也方便回顾,仅供参考ps:第一次做Windows下打包,用了2小时,第二次20秒第一次做Linux(ubuntu)下打包,用了8小时,第二次1分半一、Windows有许多比较坑的帖子,会带新人走不少弯路,大家注意鉴别(没方法,随缘)1、首先,找到......
  • Qt支持heic图片显示
    目录一、背景二、Heic图片显示heif库安装图片显示三、参考文章原文链接:Qt支持heic图片显示一、背景小孩子两岁了,最近在着手给娃做生活照纪念相册,然后就是某宝上各种聊,了解到的相册种类也是各异,价格更是良莠不齐,小几十到小几百都有,一时间还是难以下手。刚过完年那一阵偶然一次看......
  • Qt - Qt6中QTextStream类的setCodec方法没有了,怎么解决写中文文本乱码
    简介场景:程序在linux下运行,将中英文写入文本,将文本在windows上打开时,中文出现乱码 原Qt5中:QFilefile;file.open(QIODevice::WriteOnly|QIODevice::Text);QTextStreamtextStream(&file);textStream.setCodec("GBK");使用 QTextStream类的 setCodec方法即可解决上......
  • qt一个线程管理的tcp
    写一个在x线程中管理的tcp#include<QThread>#include<QTcpSocket>#include<QObject>#include<QNetworkProxy>classTcpSocketController:publicQTcpSocket{Q_OBJECTpublic:TcpSocketController(QObject*parent=nullptr):QTcpSoc......
  • qt一个在线程管理的串口
    qt一个在线程管理的串口 #include<QObject>#include<QSerialPort>#include<QSerialPortInfo>#include<QDebug>#include<QFile>#include<QTextStream>#include<QThread>#include<QTimer>#include<QEventLoop>......
  • Qt moc系统的黑魔法?
    Qt的元对象系统(Meta-ObjectSystem)是Qt框架的核心功能之一,为C++语言增加了一些动态特性,借助元对象系统Qt可以实现以下功能信号与槽机制(SignalsandSlots)运行时类型信息(Run-TimeTypeInformation,RTTI)属性系统(PropertySystem)动态对象创建(DynamicObjectCreation)动态方法调......