当前位置:网站首页>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用于获取系统中的文件路径
边栏推荐
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 挡不住了,国产芯片再度突进,部分环节已进到4nm
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- Kivy tutorial how to automatically load kV files
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- [技术发展-24]:现有物联网通信技术特点
- Conversion function and explicit
- php 迷宫游戏
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
猜你喜欢

User and group command exercises

双向链表(我们只需要关注插入和删除函数)

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
![[redis] cache warm-up, cache avalanche and cache breakdown](/img/df/81f38087704de36946b470f68e8004.jpg)
[redis] cache warm-up, cache avalanche and cache breakdown

Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm

TensorBoard可视化处理案例简析

挡不住了,国产芯片再度突进,部分环节已进到4nm

Flutter dynamic | fair 2.5.0 new version features
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点
随机推荐
logback日志的整理
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
挡不住了,国产芯片再度突进,部分环节已进到4nm
PHP maze game
Depth and breadth first traversal of tree (regardless of binary tree)
Go 1.16.4: purpose of go mod tidy
Multi table query of MySQL - multi table relationship and related exercises
Mastering the cypress command line options is the basis for truly mastering cypress
php 迷宫游戏
JVM系列——概述,程序计数器day1-1
TensorBoard可视化处理案例简析
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
Record 405 questions about bank callback post request
Thrift threadmanager and three monitors
IBEM 数学公式检测数据集
IBEM mathematical formula detection data set
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
[redis] cache warm-up, cache avalanche and cache breakdown
Unity embeddedbrowser browser plug-in event communication