当前位置:网站首页>QT learning 19 standard dialog box in QT (top)
QT learning 19 standard dialog box in QT (top)
2022-07-03 13:58:00 【A little black sauce】
Qt Study 19 Qt Standard dialog boxes in ( On )
Standard dialog box
- Qt Provides some for developers Reusable Dialog type
- Qt Reusable dialog provided The whole country does not inherit from QDialog class

- Qt Standard dialog boxes in Follow the same usage
// Define dialog objects
DialogType dlg(this);
// Set dialog properties
dlg.setPropertyXXX(value);
if(dlg.exec() == DialogType::Value) {
// Get dialog data
Type v = dlg.getDialogValue();
// Process dialog data
// ....
}
Message dialog
- Message dialog It's in the application The most common interface element
- The message dialog box is mainly used for :
- For the user Prompt important information
- Force users Make operation selection

- How to use the message dialog
// Construct message dialog object
QMessageBox msg(this);
// Set the relevant properties of the message dialog
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!";
}
File dialog
File dialog boxes are often used in the following situations
Open Mode
- In the application You need to open an external file
Save Mode
- In the application The current content needs to be stored in the external file specified by the user
How to use the file dialog
QFileDialog fd(this);
//
fd.setAcceptMode(QFileDialog::AcceptOpen);
fd.setFileMode(QFileDialog::ExistingFile);
if (fd.exec() == QFileDialog::Accepted) {
QStringList fs = fd.selectedFiles();
}
file type filter
In the file dialog box, you can Define filters through file suffixes
Filter definition rules :
Display name ( *. suffix 1 *. suffix 2 … *. suffix N )
example :“Image ( *.png *.xpm *.jpg )”
“Text ( *.txt )”
“All ( *.* )”
QFileDialog Using functions in
- QFileDialog::getOpenFileName
- QFileDialog::getOpenFileNames
- QFileDialog::getSaveFileName
Code experiments
#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!";
}
// Simple usage
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];
}
}
// Simple usage
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];
}
}
// Simple usage
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();
}
Summary
- Qt More than one Reusable Dialog type
- Inherited from QDialog type
- follow identical Usage mode
- QMessageBox Used to prompt important program information
- QFileDialog Used to get the file path in the system
边栏推荐
- Rasp implementation of PHP
- How to use lxml to judge whether the website announcement is updated
- Ocean CMS vulnerability - search php
- Golang — template
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
- Use and design of Muduo buffer class
- 消息订阅与发布
- “又土又穷”的草根高校,凭什么被称为“东北小清华”?
- jvm-类加载
猜你喜欢

Installation impression notes

Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue

Dlopen() implements dynamic loading of third-party libraries

RocksDB LRUCache

Go language web development series 27: Gin framework: using gin swagger to implement interface documents

Qt学习20 Qt 中的标准对话框(中)

Using registered classes to realize specific type matching function template

GoLand 2021.1: rename the go project

SQL Injection (GET/Search)

“又土又穷”的草根高校,凭什么被称为“东北小清华”?
随机推荐
Go 1.16.4: purpose of go mod tidy
SQL Injection (GET/Select)
从零开始的基于百度大脑EasyData的多人协同数据标注
C language standard IO function sorting
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Students who do not understand the code can also send their own token, which is easy to learn BSC
Similarities and differences of sessionstorage, localstorage and cookies
Dlopen() implements dynamic loading of third-party libraries
logback日志的整理
How to use lxml to judge whether the website announcement is updated
Rasp implementation of PHP
1px problem of mobile terminal
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
Installation impression notes
Qt学习18 登录对话框实例分析
IBEM 数学公式检测数据集
小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
JVM family - overview, program counter day1-1
掌握Cypress命令行选项,是真正掌握Cypress的基础
Qt学习20 Qt 中的标准对话框(中)