当前位置:网站首页>Qt学习19 Qt 中的标准对话框(上)
Qt学习19 Qt 中的标准对话框(上)
2022-07-03 13:23:00 【一个小黑酱】
Qt学习19 Qt 中的标准对话框(上)
标准对话框
- Qt为开发者提供了一些可复用的对话框类型
- Qt提供的可复用对话框全国不继承自QDialog类

- Qt中的标准对话框遵循相同的使用方式
// 定义对话框对象
DialogType dlg(this);
// 设置对话框属性
dlg.setPropertyXXX(value);
if(dlg.exec() == DialogType::Value) {
// 获取对话框数据
Type v = dlg.getDialogValue();
// 处理对话框数据
// ....
}
消息对话框
- 消息对话框是应用程序中最常见的界面元素
- 消息对话框主要用于:
- 为用户提示重要信息
- 强制用户进行操作选择

- 消息对话框的使用方式
// 构造消息对话框对象
QMessageBox msg(this);
// 设置消息对话框的相关属性
msg.setWindowTitle("Message Title");
msg.setText("This is message content!");
msg.setIcon(QMessageBox::Information);
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
if (msg.exec() == QMessageBox::Ok) {
qDebug() << "Ok button is clicked!";
}
文件对话框
文件对话框常用于以下情形
Open Mode
- 应用程序中需要用户打开一个外部的文件
Save Mode
- 应用程序中需要将当前内容存储与用户指定的外部文件中
文件对话框的使用方式
QFileDialog fd(this);
//
fd.setAcceptMode(QFileDialog::AcceptOpen);
fd.setFileMode(QFileDialog::ExistingFile);
if (fd.exec() == QFileDialog::Accepted) {
QStringList fs = fd.selectedFiles();
}
文件类型过滤器
在文件对话框中可以通过文件后缀定义过滤器
过滤器定义规则:
显示名 ( *.后缀1 *.后缀2 … *.后缀N )
例:“Image ( *.png *.xpm *.jpg )”
“Text ( *.txt )”
“All ( *.* )”
QFileDialog 中的使用函数
- QFileDialog::getOpenFileName
- QFileDialog::getOpenFileNames
- QFileDialog::getSaveFileName
代码实验
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton SimpleMsgBtn;
QPushButton CustomMsgBtn;
QPushButton OpenFileBtn;
QPushButton SaveFileBtn;
private slots:
void SimpleMsgBtn_Clicked();
void CustomMsgBtn_Clicked();
void OpenFileBtn_Clicked();
void SaveFileBtn_Clicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
Widget::Widget(QWidget *parent) : QWidget(parent),
SimpleMsgBtn(this), CustomMsgBtn(this), OpenFileBtn(this), SaveFileBtn(this)
{
SimpleMsgBtn.setText("Simple Message Dialog");
SimpleMsgBtn.move(20, 20);
SimpleMsgBtn.resize(160, 30);
CustomMsgBtn.setText("Custom Message Dialog");
CustomMsgBtn.move(20, 70);
CustomMsgBtn.resize(160, 30);
OpenFileBtn.setText("Open File Dialog");
OpenFileBtn.move(20, 120);
OpenFileBtn.resize(160, 30);
SaveFileBtn.setText("Save File Dialog");
SaveFileBtn.move(20, 170);
SaveFileBtn.resize(160, 30);
resize(200, 220);
connect(&SimpleMsgBtn, SIGNAL(clicked()), this, SLOT(SimpleMsgBtn_Clicked()));
connect(&CustomMsgBtn, SIGNAL(clicked()), this, SLOT(CustomMsgBtn_Clicked()));
connect(&OpenFileBtn, SIGNAL(clicked()), this, SLOT(OpenFileBtn_Clicked()));
connect(&SaveFileBtn, SIGNAL(clicked()), this, SLOT(SaveFileBtn_Clicked()));
}
Widget::~Widget()
{
}
void Widget::SimpleMsgBtn_Clicked()
{
QMessageBox msg(this);
msg.setText("This is a message dialog!");
msg.exec();
}
void Widget::CustomMsgBtn_Clicked()
{
QMessageBox msg(this);
msg.setWindowTitle("Window Title");
msg.setText("This is a detail message dialog!");
msg.setIcon(QMessageBox::Information);
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);
if (msg.exec() == QMessageBox::Ok) {
qDebug() << "Ok button is clicked!";
}
// 简便用法
QMessageBox::information(this, "Window Title", "This is a detail message dialog!", QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::YesToAll);
QMessageBox::question(this, "Question", "This is a question dialog!", QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
QMessageBox::warning(this, "Warning", "This is a warning dialog", QMessageBox::Ok);
QMessageBox::critical(this, "Warning", "This is a warning dialog!", QMessageBox::Ok);
QMessageBox::about(this, "About", "This is a about dialog!");
}
void Widget::OpenFileBtn_Clicked()
{
QFileDialog dlg(this);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setFileMode(QFileDialog::ExistingFiles);
if (dlg.exec() == QFileDialog::Accepted) {
QStringList fs = dlg.selectedFiles();
for (int i = 0; i < fs.count(); i++) {
qDebug() << fs[i];
}
}
// 简便用法
QFileDialog::getOpenFileName(this, tr("Open File"), "./", tr("Images (*.png *.xpm *.jpg)"));
QFileDialog::getOpenFileNames(this, "Select one or more files to open", "./", "Images (*.png *.xpm *.jpg)");
}
void Widget::SaveFileBtn_Clicked()
{
QFileDialog dlg(this);
dlg.setAcceptMode(QFileDialog::AcceptSave);
if (dlg.exec() == QFileDialog::Accepted) {
QStringList fs = dlg.selectedFiles();
for (int i = 0; i < fs.count(); i++) {
qDebug() << fs[i];
}
}
// 简便用法
QFileDialog::getSaveFileName(this, tr("Save File"), "./untitled.png", tr("Images (*.png *.xpm *.jpg)"));
}
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
小结
- Qt中提供了多个可复用的对话框类型
- 继承于QDialog类型
- 遵循相同使用方式
- QMessageBox用于提示重要的程序信息
- QFileDialog用于获取系统中的文件路径
边栏推荐
- CVPR 2022 | 美团技术团队精选6篇优秀论文解读
- 双向链表(我们只需要关注插入和删除函数)
- Implementation of Muduo accept connection, disconnection and sending data
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- [how to earn a million passive income]
- 记录关于银行回调post请求405 问题
- 【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
- Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
- [技术发展-24]:现有物联网通信技术特点
猜你喜欢

常见的几种最优化方法Matlab原理和深度分析

使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
![[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]](/img/95/09552d33d2a834af4d304129714775.png)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]

When updating mysql, the condition is a query

Resolved (error in viewing data information in machine learning) attributeerror: target_ names

Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Summary of common error reporting problems and positioning methods of thrift

MySQL 数据处理值增删改

TensorBoard可视化处理案例简析
随机推荐
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
JS 将伪数组转换成数组
双向链表(我们只需要关注插入和删除函数)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
【被动收入如何挣个一百万】
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Mycms we media mall v3.4.1 release, user manual update
太阳底下无新事,元宇宙能否更上层楼?
SQL Injection (AJAX/JSON/jQuery)
如何使用lxml判断网站公告是否更新
Logback log sorting
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
编程内功之编程语言众多的原因
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
When updating mysql, the condition is a query