当前位置:网站首页>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用于获取系统中的文件路径
边栏推荐
- Ocean CMS vulnerability - search php
- [redis] cache warm-up, cache avalanche and cache breakdown
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
- Conversion function and explicit
- Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
- Resource Cost Optimization Practice of R & D team
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- [understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- 物联网毕设 --(STM32f407连接云平台检测数据)
猜你喜欢
[技术发展-24]:现有物联网通信技术特点
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
JVM系列——概述,程序计数器day1-1
Go language unit test 3: go language uses gocovey library to do unit test
RichView TRVStyle ListStyle 列表样式(项目符号编号)
SQL Injection (AJAX/JSON/jQuery)
Unity EmbeddedBrowser浏览器插件事件通讯
Brief analysis of tensorboard visual processing cases
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
挡不住了,国产芯片再度突进,部分环节已进到4nm
随机推荐
[how to earn a million passive income]
树的深入和广度优先遍历(不考虑二叉树)
PHP maze game
Mastering the cypress command line options is the basis for truly mastering cypress
[技術發展-24]:現有物聯網通信技術特點
Go language unit test 3: go language uses gocovey library to do unit test
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
Depth and breadth first traversal of tree (regardless of binary tree)
The reasons why there are so many programming languages in programming internal skills
[556. Next larger element III]
Go 1.16.4: manage third-party libraries with Mod
Richview trvstyle liststyle list style (bullet number)
顺序表(C语言实现)
服务器硬盘冷迁移后网卡无法启动问题
Can newly graduated European college students get an offer from a major Internet company in the United States?
Unity EmbeddedBrowser浏览器插件事件通讯
The principle of human voice transformer