当前位置:网站首页>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用于获取系统中的文件路径
边栏推荐
- Heap structure and heap sort heapify
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- MySQL installation, uninstallation, initial password setting and general commands of Linux
- Internet of things completion -- (stm32f407 connects to cloud platform detection data)
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- Red hat satellite 6: better management of servers and clouds
- Conversion function and explicit
- [sort] bucket sort
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
猜你喜欢

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

RichView TRVStyle ListStyle 列表样式(项目符号编号)

CVPR 2022 | 美团技术团队精选6篇优秀论文解读

JVM系列——概述,程序计数器day1-1

Go language web development series 30: gin: grouping by version for routing

如何使用lxml判断网站公告是否更新

MySQL 数据处理值增删改

SQL Injection (GET/Search)

Logback log sorting

Unity embeddedbrowser browser plug-in event communication
随机推荐
[technology development-24]: characteristics of existing IOT communication technology
The principle of human voice transformer
Stack application (balancer)
Static linked list (subscript of array instead of pointer)
php 迷宫游戏
GoLand 2021.2 configure go (go1.17.6)
SQL Injection (GET/Search)
GoLand 2021.1.1: configure the multi line display of the tab of the open file
常见的几种最优化方法Matlab原理和深度分析
[技术发展-24]:现有物联网通信技术特点
The network card fails to start after the cold migration of the server hard disk
PHP maze game
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
Multi table query of MySQL - multi table relationship and related exercises
Leetcode-1175. Prime Arrangements
Leetcode-1175.Prime Arrangements
KEIL5出现中文字体乱码的解决方法
SQL Injection (GET/Select)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay