QT_ dialog box
Recommended reference
- Modal dialog box, that is, it will block the input of other windows in the same application.
Modal dialog boxes are common, such as the "open file" function. You can try to open a file in Notepad. When the open file dialog box appears, we cannot operate on windows other than this dialog box.
- For modeless dialog boxes, such as the search dialog box, we can continue to edit the contents of Notepad while displaying the search dialog box.
Dialog box classification
Qt's built-in dialog boxes are roughly divided into the following categories:
- QColorDialog: select color;
- QFileDialog: select a file or directory;
- QFontDialog: select font;
- QInputDialog: allows the user to enter a value and return it;
- QMessageBox: modal dialog box, used to display information, ask questions, etc;
- QPageSetupDialog: provides paper related options for printers;
- QPrintDialog: printer configuration;
- QPrintPreviewDialog: print preview;
- Qpprogressdialog: displays the operation process.
Custom Dialog
Qt supports modal dialog boxes and modeless dialog boxes.
Modal and non modal realization:
-
use QDialog::exec()Implement application level modal dialog boxes
-
use QDialog::open()Realize window level modal dialog box
-
use QDialog::show()Implement modeless dialog boxes.
Qt Used in QDialog Class implements the dialog box. Like the main window, we usually design a class inheritance QDialog. QDialog(And its subclasses, and all Qt::Dialog Class of type) for which parent Pointers have additional explanations: If parent by NULL,Then the dialog box will be used as a top-level window, otherwise it will be used as a child dialog box of its parent component (at this time, its default location is parent Center of). The difference between a top-level window and a non top-level window is that:/*The top-level window will have its own location in the taskbar, while the non top-level window will share the location of its parent component.*/ #include <QDialog> #include <QMessageBox> #include <QColorDialog> #include <QFileDialog> //Click the new menu item to pop up the dialog box connect(ui->actionnew,&QAction::triggered,this,[=](){ // Qdebug() < < pop up dialog box < < endl; //There are two dialog boxes //Modal dialog box: when the dialog box is open, other windows cannot be operated //Modeless dialog box: when the dialog box is open, you can operate other windows //Modal mode creation // QDialog dlg(this); // The child dialog box of the parent component. // dlg.resize(200,100); // dlg.exec(); // Blocking function application level DLG is created on the stack and is not released because of exec() blocking. // Qdebug() < < pop up dialog box < < endl; //Modeless creation without blocking // QDialog dlg2(this); // Create it on the stack and flash it without blocking // dlg2->show(); // Flash by without blocking. After the show function, the Lambda expression ends and exits, and the dlg2 local object is destroyed. The dialog box disappears. // QDialog * dlg2 = new QDialog(this); // The new this object is on the heap and created into the children table (this) of the widget. In this way, it will be released only when the widget window is closed!!! Only the decomposition of this object will be released, where this is the entire widget window. If there is no this dependency (qdialog * dlg2 = new qdialog), the created object needs to be deleted manually (delete QDialog). // dlg2->resize(200,100); // dlg2->show(); // //Set the properties of dlg2 // dlg2->setAttribute(Qt::WA_DeleteOnClose); // When clicking the close button in the dialog box, the memory will be released //Set standard dialog box QMessageBox // QMessageBox::critical(this, "error", "critical"); // QMessageBox::information(this, "information", "info"); //Parameter 1 father, 2: title 3: prompt content 4: key type 5: key associated with enter // QMessageBox::question(this, "query dialog", "question",QMessageBox::Save | QMessageBox::Cancel); // if(QMessageBox::Save == QMessageBox::question(this, "query dialog", "question",QMessageBox::Save | QMessageBox::Cancel)) // { // Qdebug() < < "Click Save" < < endl; // } // else { // Qdebug() < < "click Cancel" < < endl; // } //Select color dialog box QColor color_choose = QColorDialog::getColor(QColor(255,0,0)); qDebug()<<color_choose.red()<<color_choose.blue()<<color_choose.green(); //File dialog box, caption: Title dir: path filter: which types of files are displayed (filtering function) // QFileDialog::getOpenFileName(this, "open file", "d:\00\u mydate\qt", "(*.txt *.png)")// Note. Change to double slash // Qstring path = qfiledialog:: GetOpenFilename (this, "open file", "d:\00\u mydate\qt", "(*.txt *.png)"); // qDebug()<<path; QString name = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("Please select a video file")); //fromLocal8Bit: the default code may be garbled in Chinese if (name.isEmpty())return; //If no file is selected, the name is empty! string file = name.toLocal8Bit().data(); //Chinese conversion, non Chinese files are OK. Chinese should be converted to utf8 format through toLocal8Bit.
1. Use of file dialog box
#include <QFileDialog> #include <string> #include <QMessageBox> //Routine 1 QString curPath=QDir::currentPath();//Get the current directory of the system //Get the path of the application QString dlgTitle="Select a file"; //Dialog Title QString filter="text file(*.txt);;Picture file(*.jpg *.gif *.png);;All documents(*.*)"; //File filter QString aFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter); if (!aFileName.isEmpty()) ui->plainTextEdit->appendPlainText(aFileName); } //Routine 2 void XVideoUI::Open() { QString name = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("Please select a video file")); //fromLocal8Bit: the default code may be garbled in Chinese if (name.isEmpty())return; string file = name.toLocal8Bit().data(); //Chinese conversion, non Chinese files are OK. Chinese should be converted to utf8 format through toLocal8Bit. //QMessageBox::information(this, "", name); if (!XVideoThread::Get()->Open(file)) { QMessageBox::information(this, "error", name + " open failed!"); return; } Play(); //Turn on default playback } //Open No.1 video source file static VideoCapture cap1;//No.1 video source bool XVideoThread::Open(const std::string file) { cout << "open :" << file << endl; mutex.lock(); bool re = cap1.open(file); mutex.unlock(); cout << re << endl; if (!re) return re; fps = cap1.get(CAP_PROP_FPS); //Get fps width = cap1.get(CAP_PROP_FRAME_WIDTH); //Get the width and height of the video height = cap1.get(CAP_PROP_FRAME_HEIGHT); if (fps <= 0) fps = 25; //In case no value is obtained. Then take a custom value src1file = file; //Record file name when opening double count = cap1.get(CAP_PROP_FRAME_COUNT); totalMs = (count / (double)fps) * 1000; //Total frames /fps = second duration, converted to milliseconds return true; }
2. Color dialog box
QColorDialog dialog
QColorDialog is a dialog box for selecting colors, which uses the static function QColorDialog::getColor(). Here is
The code of the "select color" button, which selects the color for the font of the text box.
void Dialog::on_btnColor_clicked() { QPalette pal=ui->plainTextEdit->palette(); //Get existing palette QColor iniColor=pal.color(QPalette::Text); //Existing text color QColor color=QColorDialog::getColor(iniColor,this,"Choose a color"); if (color.isValid()) //Selection is valid { pal.setColor(QPalette::Text,color); //palette set the selected color ui->plainTextEdit->setPalette(pal); //Set palette } }
3. Font dialog box
QFontDialog is a dialog box for selecting fonts. Selecting fonts uses the static function QFontDialog::getFont(). Below is "select"
The code of the "select font" button, which selects a font for the text box. The font settings include font name, size, bold
Italics, etc.
void Dialog::on_btnFont_clicked() {//Select font QFont iniFont=ui->plainTextEdit->font(); //Get the font of the text box bool ok=false; QFont font=QFontDialog::getFont(&ok,iniFont); //Select font if (ok) //Selection is valid ui->plainTextEdit->setFont(font); }
4. Standard input dialog box
QInputDialog standard input dialog box
QInputDialog has many input methods, such as single line string input, integer input, floating-point number input, list box selection input and multi line text. Figure 3 shows the four interface effects.
void Dialog::on_btnInputString_clicked() { //Input string QString dlgTitle="Input text dialog box"; QString txtLabel="Please enter a file name"; QString defaultInput="new file.txt"; QLineEdit::EchoMode echoMode=QLineEdit::Normal;//Normal text input //QLineEdit::EchoMode echoMode=QLineEdit::Password;// Password input bool ok=false; QString text = QInputDialog::getText(this, dlgTitle,txtLabel, echoMode,defaultInput, &ok); if (ok && !text.isEmpty()) ui->plainTextEdit->appendPlainText(text); }
5. Message dialog box
Simple information tips
The message dialog box QMessageBox is used to display prompt, warning, error and other information, or to confirm and select. These functions are realized by several static functions. Among them, the input parameters and use methods of warning(), information(), critical(), and about() functions are the same, but the icons of information prompts are different.
void Dialog::on_btnMsgInformation_clicked() { QString dlgTitle="information message box"; QString strInfo="The file has been opened and the font size has been set"; QMessageBox::information(this, dlgTitle, strInfo, QMessageBox::Ok,QMessageBox::NoButton); } void Dialog::on_btnMsgWarning_clicked() { QString dlgTitle="warning message box"; QString strInfo="The file content has been modified"; QMessageBox::warning(this, dlgTitle, strInfo); } void Dialog::on_btnMsgCritical_clicked() { QString dlgTitle="critical message box"; QString strInfo="Unknown program accessing the network"; QMessageBox::critical(this, dlgTitle, strInfo); } void Dialog::on_btnMsgAbout_clicked() { QString dlgTitle="about message box"; QString strInfo="Data viewing software developed by me V1.0 \n All rights reserved"; QMessageBox::about(this, dlgTitle, strInfo); }
6. Customize dialog box
Inherited from QDialog dialog box
class QWDialogSize : public QDialog { Q_OBJECT public: explicit QWDialogSize(QWidget *parent = 0); ~QWDialogSize(); int rowCount();//Get the number of rows entered in the dialog box int columnCount();//Get the number of columns entered in the dialog box void setRowColumn(int row, int column); //Values of the two spinboxes on the initial dialog private slots: private: Ui::QWDialogSize *ui; };