当前位置: 首页 > news >正文

Qt (QInputDialog 、QMessageBox、QMessageBox)对话框实战

目录

一、QInputDialog 类(输入对话框)

二、QMessageBox 类(消息框)

三、QMessageBox 类(自定义消息框)


一、QInputDialog 类(输入对话框)

QInputDialog 是一个提供输入对话框的 Qt 类。它允许用户输入文本,并提供给用户选择可用选项的选项列表。QInputDialog 可以显示多种输入类型的对话框,例如:

  1. 文本输入框:允许用户输入文本;
  2. 整数输入框:仅允许用户输入整数;
  3. 浮点数输入框:仅允许用户输入浮点数;
  4. 列表框:显示可用选项的选项列表,用户可以从中选择一个选项。

QInputDialog 通过静态函数调用。例如,要显示一个文本输入框,可以使用以下代码:

QString text = QInputDialog::getText(this, "Input Dialog", "Enter your name:");

此代码将显示一个对话框,提示用户输入文本,并将用户输入保存在 text 变量中。除了 getText() 函数之外,还有其他静态函数可用于显示整数和浮点数输入框以及列表框。

案例分析:实现下图功能

dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>#include <QInputDialog>class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();private:QGridLayout *glayout;QPushButton *inputstudentnobutton;QLineEdit *inputstudentnobuttonLineEdit;QPushButton *inputstudentnamebutton;QLineEdit *inputstudentnamebuttonLineEdit;QPushButton *inputstudentsexbutton;QLineEdit *inputstudentsexbuttonLineEdit;QPushButton *inputstudentscorebutton;QLineEdit *inputstudentscorebuttonLineEdit;private slots:void  modifystudentno();void  modifystudentsex();};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent)
{resize(260,110);setWindowTitle("输入对话框测试");// 布局glayout=new QGridLayout(this);inputstudentnobutton=new QPushButton;inputstudentnobutton->setText("学生学号:");inputstudentnobuttonLineEdit=new QLineEdit("20220370218");inputstudentnamebutton=new QPushButton;inputstudentnamebutton->setText("学生姓名:");inputstudentnamebuttonLineEdit=new QLineEdit("林佳欣");inputstudentsexbutton=new QPushButton;inputstudentsexbutton->setText("学生性别:");inputstudentsexbuttonLineEdit=new QLineEdit("女");inputstudentscorebutton=new QPushButton;inputstudentscorebutton->setText("学生分数:");inputstudentscorebuttonLineEdit=new QLineEdit("99");glayout->addWidget(inputstudentnobutton,0,0);glayout->addWidget(inputstudentnobuttonLineEdit,0,1);glayout->addWidget(inputstudentnamebutton,1,0);glayout->addWidget(inputstudentnamebuttonLineEdit,1,1);glayout->addWidget(inputstudentsexbutton,2,0);glayout->addWidget(inputstudentsexbuttonLineEdit,2,1);glayout->addWidget(inputstudentscorebutton,3,0);glayout->addWidget(inputstudentscorebuttonLineEdit,3,1);connect(inputstudentnobutton,SIGNAL(clicked()),this,SLOT(modifystudentno()));connect(inputstudentsexbutton,SIGNAL(clicked()),this,SLOT(modifystudentsex()));
}Dialog::~Dialog()
{
}void  Dialog::modifystudentno()
{bool isbool;QString strText=QInputDialog::getText(this,"标准输入对话框","请输入学号:",QLineEdit::Normal,inputstudentnobuttonLineEdit->text(),&isbool);if(isbool && !strText.isEmpty()){inputstudentnobuttonLineEdit->setText(strText);}
}void  Dialog::modifystudentsex()
{QStringList strSexItems;strSexItems<<"男"<<"女";bool isbool;QString strsexItem=QInputDialog::getItem(this,"标准输入对话框","请选择性别:",strSexItems,0,false,&isbool);if(isbool && !strsexItem.isEmpty()){inputstudentsexbuttonLineEdit->setText(strsexItem);}}

main.cpp

#include "dialog.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}

二、QMessageBox 类(消息框)

QMessageBox 类是一个交互式的对话框类,用于显示一个或多个按钮、图标和文本消息,以便用户根据需要进行选择。它是 Qt 中的一个标准对话框类,提供了许多方便的方法来创建、显示和处理对话框。

QMessageBox 类的常用函数包括:

  1. QMessageBox():构造函数,创建一个空的 QMessageBox 对象。

  2. setText():设置消息对话框中的文本内容。

  3. setIcon():设置消息对话框中的图标。

  4. addButton():向消息对话框中添加按钮。

  5. exec():执行消息对话框,并返回用户选择的按钮 ID。

使用 QMessageBox 类可以快速创建常见的对话框,如询问用户是否确定某个操作、提醒用户操作出错等。它是 Qt 应用中常用的基础对话框之一。

案例分析:实现下图功能

代码示例:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();private:QGridLayout *glayout;QLabel *displabel;QPushButton *questionbutton; // 问题消息框命令按钮QPushButton *informationbutton; // 信息消息框命令按钮QPushButton *warningbutton; // 警告消息框命令按钮QPushButton *criticalbutton; // 错误消息框命令按钮QPushButton *aboutbutton; // 关于消息框命令按钮QPushButton *aboutqtbutton; //private slots:void displayquestionMsg();void displayinformationMsg();void displaywarningMsg();void displaycriticalMsg();void displayaboutMsg();void displayaboutqtMsg();};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent)
{setWindowTitle("消息框测试");resize(320,150);glayout=new QGridLayout(this);displabel=new QLabel("请你选择一个消息框");questionbutton=new QPushButton("questionMsg"); // 问题消息框命令按钮informationbutton=new QPushButton("informationMsg"); // 信息消息框命令按钮warningbutton=new QPushButton("warningMsg"); // 警告消息框命令按钮criticalbutton=new QPushButton("criticalMsg"); // 错误消息框命令按钮aboutbutton=new QPushButton("aboutMsg"); // 关于消息框命令按钮aboutqtbutton=new QPushButton("aboutQtMsg"); //glayout->addWidget(displabel,0,0,1,2);glayout->addWidget(questionbutton,1,0);glayout->addWidget(informationbutton,1,1);glayout->addWidget(warningbutton,2,0);glayout->addWidget(criticalbutton,2,1);glayout->addWidget(aboutbutton,3,0);glayout->addWidget(aboutqtbutton,3,1);connect(questionbutton,SIGNAL(clicked()),this,SLOT(displayquestionMsg()));connect(informationbutton,SIGNAL(clicked()),this,SLOT(displayinformationMsg()));connect(warningbutton,SIGNAL(clicked()),this,SLOT(displaywarningMsg()));connect(criticalbutton,SIGNAL(clicked()),this,SLOT(displaycriticalMsg()));connect(aboutbutton,SIGNAL(clicked()),this,SLOT(displayaboutMsg()));connect(aboutqtbutton,SIGNAL(clicked()),this,SLOT(displayaboutqtMsg()));}Dialog::~Dialog()
{
}void Dialog::displayquestionMsg()
{displabel->setText("question QMessageBox");switch(QMessageBox::question(this,"Question消息框","你是否想退出程序应用,请选择?",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok)){case QMessageBox::Ok:displabel->setText("你选择questionMsg命令按钮当中的button/Ok!");break;case QMessageBox::Cancel:displabel->setText("你选择questionMsg命令按钮当中的button/Cancel!");break;default:break;}return ;
}void Dialog::displayinformationMsg()
{displabel->setText("information QMessageBox");QMessageBox::information(this,"Information消息框","Information消息框测试成功,大家可以自己描述");return ;
}
void Dialog::displaywarningMsg()
{displabel->setText("warning QMessageBox");switch(QMessageBox::warning(this,"Warning消息框","是否删除数据库sudent.mdb,请注意数据的操作安全?",QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save)){case QMessageBox::Save:displabel->setText("你选择warningMsg命令按钮当中的button/Save!");break;case QMessageBox::Discard:displabel->setText("你选择warningMsg命令按钮当中的button/Discard!");break;case QMessageBox::Cancel:displabel->setText("你选择warningMsg命令按钮当中的button/Cancel!");break;default:break;}return ;}
void Dialog::displaycriticalMsg()
{displabel->setText("critical QMessageBox");QMessageBox::critical(this,"critical消息框","数据库文件备份错误,请重新检查?");return ;}
void Dialog::displayaboutMsg()
{displabel->setText("about QMessageBox");QMessageBox::about(this,"about消息框","测试Qt about消息框");return ;}
void Dialog::displayaboutqtMsg()
{displabel->setText("aboutQt QMessageBox");QMessageBox::aboutQt(this,"aboutQt消息框测试--罗小白的干爹");return ;}

main.cpp

#include "dialog.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}

三、QMessageBox 类(自定义消息框)

在 Qt 中,可以使用 QMessageBox 类创建标准的消息框,但是如果需要自定义消息框,可以通过以下步骤实现:

  1. 创建一个 QDialog 类型的窗口,该窗口将作为自定义消息框。
  2. 在窗口中添加 QLabel 控件用于显示消息文字。
  3. 在窗口中添加自定义按钮,如确定、取消等。
  4. 通过信号和槽连接自定义按钮的点击事件,以在用户点击按钮时执行相应的操作。
  5. 在需要使用自定义消息框的地方,实例化该 QDialog 类型的窗口并显示。

案例分析:实现下图功能

代码示例:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>#include <QMessageBox>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();private:QLabel *labelmsg,*labeldispmsg;QPushButton *msgbutton;QGridLayout *glayout;private slots:void customMsg();
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent)
{setWindowTitle("自定义消息框测试");resize(260,90);glayout=new QGridLayout(this);labelmsg=new QLabel("自定义消息框");msgbutton=new QPushButton("测试操作");labeldispmsg=new QLabel("未测试状态");glayout->addWidget(labelmsg,0,0);glayout->addWidget(msgbutton,0,1);glayout->addWidget(labeldispmsg,1,0,1,1);connect(msgbutton,SIGNAL(clicked()),this,SLOT(customMsg()));}Dialog::~Dialog()
{
}void Dialog::customMsg()
{QMessageBox cMsgBox;cMsgBox.setWindowTitle("zgl的消息框");// 添加按钮QPushButton *yes=cMsgBox.addButton("YES",QMessageBox::ActionRole);QPushButton *no=cMsgBox.addButton("NO",QMessageBox::ActionRole);// 添加图标cMsgBox.setIconPixmap(QPixmap("d:\\lspng.png"));cMsgBox.exec();// 判断用户点击按钮 yes noif(cMsgBox.clickedButton()==yes){labeldispmsg->setText("用户点击YES按钮");}else if(cMsgBox.clickedButton()==no){labeldispmsg->setText("用户点击NO按钮");}}

main.cpp

#include "dialog.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}

相关文章:

Qt (QInputDialog 、QMessageBox、QMessageBox)对话框实战

目录 一、QInputDialog 类(输入对话框) 二、QMessageBox 类(消息框) 三、QMessageBox 类(自定义消息框) 一、QInputDialog 类(输入对话框) QInputDialog 是一个提供输入对话框的 Qt 类。它允许用户输入文本&#xff0c;并提供给用户选择可用选项的选项列表。QInputDialog 可…...

Java 解析 cURL(bash) 命令

解析 cURL&#xff08;bash&#xff09; 命令 1. 主要用于解析从浏览器复制来的 cURL(bash)2. 废话不多说&#xff0c;都在&#x1f37b;代码里了。参考资料 1. 主要用于解析从浏览器复制来的 cURL(bash) curl https://eva2.csdn.net/v3/06981375190026432f77c01bfca33e32/lts/…...

JDK21的虚拟线程是什么?和平台线程什么关系?

虚拟线程&#xff08;Virtual Thread&#xff09;是 JDK 而不是 OS 实现的轻量级线程(Lightweight Process&#xff0c;LWP&#xff09;&#xff0c;由 JVM 调度。许多虚拟线程共享同一个操作系统线程&#xff0c;虚拟线程的数量可以远大于操作系统线程的数量。 在引入虚拟线程…...

Unity DOTS Component概述

最近DOTS终于发布了正式的版本, 我们来分享以下DOTS里面地几个关键概念&#xff0c;方便大家上手学习掌握Unity DOTS开发。 Unity DOTS 中Entity作为实体不直接去存放数据&#xff0c;而是将数据以一个个的组件为载体来存放起来。每个Entity会得到一些不同的ComponentData的组…...

element ui 下拉框 选择月份和天数

一、背景 目前做的管理系统项目&#xff0c;期望实现功能为&#xff1a;设置出账周期和出账日&#xff0c;考虑使用element ui下拉框实现功能 二、所用技术 vue2element ui 三、实现效果 四、具体代码 <template><popup-frame :title"批量设置出账日" …...

用Java包com.sun.net.httpserver下面的类实现一个简单的http服务器demo

java的com.sun.net.httpserver包下的类提供了一个高层级的http服务器API&#xff0c;可以用来构建内嵌的http服务器。支持http和https。这些API提供了一个RFC 2616 (HTTP 1.1)和RFC 2818 (HTTP over TLS)的部分实现。 https://docs.oracle.com/en/java/javase/19/docs/api/jdk.…...

unity 浏览器插件【embedded browser(原zfbrowser)】简单教程,使unity支持web h5页面,附软件下载链接

一 简介 这是个在项目中使用了很久的浏览器插件。 很负责任的说这是在pc平台上最好用的浏览器插件 商业付费价格78刀&#xff0c;相比3d webview等插件动不动就178、368的价格就显得很良心 最新版下载链接&#xff08;请勿商用&#xff09; 1.1 功能概述 基本和普通浏览器无…...

LeetCode算法位运算—只出现一次的数字

目录 136. 只出现一次的数字 - 力扣&#xff08;LeetCode&#xff09; 解题思路&#xff1a; 代码&#xff1a; 运行结果&#xff1a; 补充 异或的重要性质 136. 只出现一次的数字 - 力扣&#xff08;LeetCode&#xff09; 给你一个 非空 整数数组 nums &#xff0c;除了某…...

vcpkg manifest 的使用

最近项目上要使用 CMakeLists 管理&#xff0c;由于 Windows 版本有依赖到 vcpkg 提供的库&#xff0c;所以需要使用 vcpkg manifest 来统一设置库的版本&#xff0c;方便后续维护 推荐一个文章&#xff0c;介绍的可以说是非常全面了 VCPKG 特性 - Versioning 不过里面也有一些…...

选择什么电容笔比较好?平板手写笔推荐

由于苹果Pencil的热销&#xff0c;让华国内市场上&#xff0c;也出现了不少的平替式电容笔&#xff0c;这些产品&#xff0c;有好有坏&#xff0c;价格也很公道。不过&#xff0c;也有很多产品的价格都很平价。我是一个拥有多年经验的数码发烧友&#xff0c;在前几年就开始用上…...

pdf转二维码怎么做?pdf二维码制作简单技巧

pdf是一种很常见的文件储存格式&#xff0c;一般通知、发票、简历都会保存为这种格式来使用&#xff0c;那么需要将pdf格式文件做成二维码&#xff0c;该用什么方式来制作呢&#xff1f;下面给大家分享一个pdf转二维码的在线工具&#xff0c;可以通过上传文件一键生成二维码&am…...

【CANoe】TX Self-ACK自应答配置与CPAL实现

一、引言 在测试CAN&CANFD通信或者网络管理的时候&#xff0c;我们经常遇到使用报文&#xff08;网络管理报文或者通信报文&#xff09;唤醒被测件这个测试点&#xff0c;如果测试比较多的情况下&#xff0c;我们就会发现&#xff0c;如果CANoe没有接被测件或者被测件没有…...

(Python)MATLAB mat矩阵和Python npy矩阵转换

Python np.ndarray矩阵转换为MATLAB mat文件 import numpy as npimport scipy.io as iomat_path mat_save_pathmat np.zeros([6, 128])io.savemat(mat_path, {name: mat})Python读取MATLAB mat文件 import numpy as np from scipy import iomat io.loadmat(your_mat_file.…...

Flink1.14 SourceReader概念入门讲解与源码解析 (三)

目录 SourceReader 概念 SourceReader 源码方法 void start(); InputStatus pollNext(ReaderOutput output) throws Exception; List snapshotState(long checkpointId); CompletableFuture isAvailable(); void addSplits(List splits); 参考 SourceReader 概念 Sour…...

PS运行中缺失d3dcompiler_47.dll问题的5个有效修复方法总结

在使用ps作图的时候&#xff0c;我们有时会遇到一些问题&#xff0c;其中之一就是“PS运行中缺失d3dcompiler_47.dll”的问题。这个问题可能会导致PS无法正常运行&#xff0c;“d3dcompiler_47.dll”。这是一个动态链接库文件&#xff0c;它是DirectX的一部分&#xff0c;主要负…...

【MATLAB-Retinex图像增强算法的去雾技术】

续&#xff1a;【MATLAB-基于直方图优化的图像去雾技术】 【MATLAB-Retinex图像增强算法的去雾技术】 1 原图2 MATLAB实现代码3 结果图示 参考书籍&#xff1a;计算机视觉与深度学习实战:以MATLAB、Python为工具&#xff0c; 主编&#xff1a;刘衍琦, 詹福宇&#xff0c; 王德建…...

使用 2 个 HSplitView 在 swiftUI 中创建一个 3 窗格界面

Pet*_*ter 8 嗯&#xff0c;我会的。在断断续续地挣扎了几个星期之后&#xff0c;在我问这个问题一个小时后&#xff0c;我似乎解决了它&#xff01;只需将第二个 HSplitView 的 layoutPriority 设置为 1&#xff0c;并将中心视图也设置为 1。当你想到它时是有道理的&#xff1…...

【C++ 操作符重载:定制自己的运算符行为】

在C编程中&#xff0c;操作符重载是一项强大的特性&#xff0c;它允许程序员定制内置运算符的行为&#xff0c;使它们适用于用户自定义的数据类型。这篇博客将介绍什么是操作符重载&#xff0c;如何使用它&#xff0c;以及一些最佳实践。 什么是操作符重载&#xff1f; 操作符…...

Android Fragment 基本概念和基本使用

Android Fragment 基本概念和基本使用 一、基本概念 Fragment&#xff0c;简称碎片&#xff0c;是Android 3.0&#xff08;API 11&#xff09;提出的&#xff0c;为了兼容低版本&#xff0c;support-v4库中也开发了一套Fragment API&#xff0c;最低兼容Android 1.6。 过去s…...

xml schema中的all元素

说明 xml schema中的all元素表示其中的子元素可以按照任何顺序出现&#xff0c;每个元素可以出现0次或者1次。 https://www.w3.org/TR/xmlschema-1/#element-all maxOccurs的默认值是1&#xff0c;minOccurs 的默认值是1。 举例 <element name"TradePriceRequest&…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

树莓派超全系列教程文档--(61)树莓派摄像头高级使用方法

树莓派摄像头高级使用方法 配置通过调谐文件来调整相机行为 使用多个摄像头安装 libcam 和 rpicam-apps依赖关系开发包 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 配置 大多数用例自动工作&#xff0c;无需更改相机配置。但是&#xff0c;一…...

渲染学进阶内容——模型

最近在写模组的时候发现渲染器里面离不开模型的定义,在渲染的第二篇文章中简单的讲解了一下关于模型部分的内容,其实不管是方块还是方块实体,都离不开模型的内容 🧱 一、CubeListBuilder 功能解析 CubeListBuilder 是 Minecraft Java 版模型系统的核心构建器,用于动态创…...

postgresql|数据库|只读用户的创建和删除(备忘)

CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

MySQL中【正则表达式】用法

MySQL 中正则表达式通过 REGEXP 或 RLIKE 操作符实现&#xff08;两者等价&#xff09;&#xff0c;用于在 WHERE 子句中进行复杂的字符串模式匹配。以下是核心用法和示例&#xff1a; 一、基础语法 SELECT column_name FROM table_name WHERE column_name REGEXP pattern; …...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

C# 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...