当前位置:网站首页>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
边栏推荐
- Qt学习25 布局管理器(四)
- Uniapp tips - scrolling components
- 如何使用lxml判断网站公告是否更新
- 交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳
- Flutter dynamic | fair 2.5.0 new version features
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Implementation of Muduo accept connection, disconnection and sending data
- JVM family - overview, program counter day1-1
- How to promote the progress of project collaboration | community essay solicitation
- Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ
猜你喜欢
Qt学习22 布局管理器(一)
logback日志的整理
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
GoLand 2021.2 configure go (go1.17.6)
jvm-运行时数据区
Flutter dynamic | fair 2.5.0 new version features
Universal dividend source code, supports the dividend of any B on the BSC
Failure of vector insertion element iterator in STL
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
掌握Cypress命令行选项,是真正掌握Cypress的基础
随机推荐
Use and design of Muduo buffer class
Qt学习19 Qt 中的标准对话框(上)
logback日志的整理
Golang - command line tool Cobra
[556. Next larger element III]
JVM系列——概述,程序计数器day1-1
从零开始的基于百度大脑EasyData的多人协同数据标注
Flutter dynamic | fair 2.5.0 new version features
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
SQL Injection (GET/Search)
JVM family - overview, program counter day1-1
Golang — template
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
怎样删除对象的某个属性或⽅法
SQL Injection (GET/Select)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
Go language unit test 4: go language uses gomonkey to test functions or methods
Multi person collaborative data annotation based on Baidu brain easydata from scratch
信创产业现状、分析与预测
Implementation of Muduo accept connection, disconnection and sending data