当前位置:网站首页>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用于获取系统中的文件路径
边栏推荐
- Go language unit test 3: go language uses gocovey library to do unit test
- MySQL functions and related cases and exercises
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- GoLand 2021.2 configure go (go1.17.6)
- [quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
- php 迷宫游戏
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- Golang - command line tool Cobra
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
猜你喜欢

3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)

Universal dividend source code, supports the dividend of any B on the BSC

Flutter dynamic | fair 2.5.0 new version features

Flutter动态化 | Fair 2.5.0 新版本特性

Screenshot of the operation steps of upload labs level 4-level 9

Common network state detection and analysis tools

KEIL5出现中文字体乱码的解决方法

Libuv Library - Design Overview (Chinese version)
![[technology development-24]: characteristics of existing IOT communication technology](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[technology development-24]: characteristics of existing IOT communication technology

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
随机推荐
Father and basketball
Open PHP error prompt under Ubuntu 14.04
Kivy教程之 如何自动载入kv文件
静态链表(数组的下标代替指针)
双向链表(我们只需要关注插入和删除函数)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
[technology development-24]: characteristics of existing IOT communication technology
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
MySQL 数据处理值增删改
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
Swiftui development experience: the five most powerful principles that a programmer needs to master
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
PHP maze game
GoLand 2021.1.1: configure the multi line display of the tab of the open file
JSON serialization case summary
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Common network state detection and analysis tools
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
记录关于银行回调post请求405 问题