当前位置: 首页 > 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&…...

【解密LSTM、GRU如何解决传统RNN梯度消失问题】

解密LSTM与GRU&#xff1a;如何让RNN变得更聪明&#xff1f; 在深度学习的世界里&#xff0c;循环神经网络&#xff08;RNN&#xff09;以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而&#xff0c;传统RNN存在的一个严重问题——梯度消失&#…...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

DBAPI如何优雅的获取单条数据

API如何优雅的获取单条数据 案例一 对于查询类API&#xff0c;查询的是单条数据&#xff0c;比如根据主键ID查询用户信息&#xff0c;sql如下&#xff1a; select id, name, age from user where id #{id}API默认返回的数据格式是多条的&#xff0c;如下&#xff1a; {&qu…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

【7色560页】职场可视化逻辑图高级数据分析PPT模版

7种色调职场工作汇报PPT&#xff0c;橙蓝、黑红、红蓝、蓝橙灰、浅蓝、浅绿、深蓝七种色调模版 【7色560页】职场可视化逻辑图高级数据分析PPT模版&#xff1a;职场可视化逻辑图分析PPT模版https://pan.quark.cn/s/78aeabbd92d1...

并发编程 - go版

1.并发编程基础概念 进程和线程 A. 进程是程序在操作系统中的一次执行过程&#xff0c;系统进行资源分配和调度的一个独立单位。B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。C.一个进程可以创建和撤销多个线程;同一个进程中…...

医疗AI模型可解释性编程研究:基于SHAP、LIME与Anchor

1 医疗树模型与可解释人工智能基础 医疗领域的人工智能应用正迅速从理论研究转向临床实践,在这一过程中,模型可解释性已成为确保AI系统被医疗专业人员接受和信任的关键因素。基于树模型的集成算法(如RandomForest、XGBoost、LightGBM)因其卓越的预测性能和相对良好的解释性…...

MLP实战二:MLP 实现图像数字多分类

任务 实战&#xff08;二&#xff09;&#xff1a;MLP 实现图像多分类 基于 mnist 数据集&#xff0c;建立 mlp 模型&#xff0c;实现 0-9 数字的十分类 task: 1、实现 mnist 数据载入&#xff0c;可视化图形数字&#xff1b; 2、完成数据预处理&#xff1a;图像数据维度转换与…...

【笔记】结合 Conda任意创建和配置不同 Python 版本的双轨隔离的 Poetry 虚拟环境

如何结合 Conda 任意创建和配置不同 Python 版本的双轨隔离的Poetry 虚拟环境&#xff1f; 在 Python 开发中&#xff0c;为不同项目配置独立且适配的虚拟环境至关重要。结合 Conda 和 Poetry 工具&#xff0c;能高效创建不同 Python 版本的 Poetry 虚拟环境&#xff0c;接下来…...

多模态大语言模型arxiv论文略读(112)

Assessing Modality Bias in Video Question Answering Benchmarks with Multimodal Large Language Models ➡️ 论文标题&#xff1a;Assessing Modality Bias in Video Question Answering Benchmarks with Multimodal Large Language Models ➡️ 论文作者&#xff1a;Jea…...