The full name of the function tr() is QObject::tr(), and the strings processed by it can be extracted by tools and translated into other languages, that is, for international use.
Just remember Qt's best practice: if you want to internationalize your program, all user visible strings should use QObject::tr()!
However, why don't we write "QObject::tr()" instead of "tr()"? Originally, the tr() function is defined in the {Object, and} q is used for all functions_ All classes of the Object} macro automatically have the} tr() function.
If you want to internationalize your program, write all user visible strings into tr(), which can be directly translated into multilingual versions with tools
catalogue
- 1, Color dialog
- 2, File dialog
- 3, Font dialog box
- 4, Input dialog box
- 5, Message dialog box
- 6, Progress dialog box
- 7, Error message dialog box
- 8, Wizard dialog box
Qt provides some common dialog types, all inherited from QDialog class, and added its own characteristic functions, such as obtaining color, displaying specific information, etc.
1, Color dialog
The color dialog class QColorDialog provides a dialog part that can obtain the specified color.
/*** The first way***/ //QColor color = QColorDialog::getColor(Qt::red, this, tr("color dialog"), QColorDialog::ShowAlphaChannel); //qDebug() <<"color:" <<color; /*** The second way***/ QColorDialog dialog(Qt::red, this); // create object dialog.setOption(QColorDialog::ShowAlphaChannel); // Show alpha options dialog.exec(); // Run dialog in modal mode QColor color = dialog.currentColor(); // Get color dialog current color qDebug() <<"color:" <<color; // Output color information
The first method uses QColorDialog's static function getColor() to obtain color. Its three parameters are used to set the initial color, parent window and dialog title respectively. The advantage of the first method is that you don't have to create objects. However, if you want to set more flexibly, use the second method, first create objects, and then make various settings. The implementation effect of the two is equivalent.
2, File dialog
The file dialog class QFileDialog provides a dialog box that allows users to select files or folders.
QString fileName = QFileDialog::getOpenFileName(this, tr("File dialog"), "F:",tr("Picture file(* png * jpg)"));
- The four parameters of this function are to specify the parent window, set the dialog box title, specify the default open directory path, and set the file type filter. If you do not specify a file filter, all types of files are selected by default.
- Only png and jpg image files are selected here (note that a space is required between * png and * jpg in the code), then only these two formats of files in the directory can be displayed in the open file dialog box.
You can also set multiple filters of different categories, and use two semicolons ";" between different categories Separate, such as adding a text file type:
QString fileName = QFileDialog::getOpenFileName(this, tr("File dialog"), "F:",tr("Picture file(* png * jpg);;text file(* txt)"));
The previous program can only select a single file. To select multiple files at the same time, you can use the getOpenFileNames() function, for example:
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("File dialog"), "F:",tr("Picture file(* png * jpg);;text file(* txt)"));
In addition to the above two functions, the QFileDialog class also provides the getSaveFileName() function to implement the save file dialog box and the save file as dialog box, and the getExistingDirectory() function to obtain an existing folder path.
3, Font dialog box
The font dialog box QFontDialog class provides a dialog box part that can select a font.
// OK is used to mark whether the OK button was clicked. Then get the selected font bool ok; QFont font = QFontDialog::getFont(&ok, this); // If you click the Cancel button, change the font if (ok) ui->pushButton_3->setFont(font); else qDebug() <<tr("No font selected!");
Here, the static function getFont() of the QFileDidog class is used to obtain the selected font. The first parameter is a bool type variable, which is used to store the status of the pressed button. For example, if you click the OK button in the open font dialog box, the OK here is true, so as to tell the user that the font has been selected.
4, Input dialog box
The input dialog box QInputDialog class is used to provide a simple and convenient dialog box to obtain a single value or string from the user.
bool ok; // Get string QString string = QInputDialog::getText(this, tr("Input string dialog box"),tr("Please enter user name:"),QLineEdit::Normal, tr("admin"), &ok); if(ok) qDebug() <<tr("string:") <<string; // Get integer int value1 = QInputDialog::getInt(this, tr("Enter integer dialog box"),tr("Please enter -1000 Values between and 1000"), 100, -1000, 1000, 10, &ok); if(ok) qDebug() <<tr("value1:") <<value1; // Get floating point number double value2 = QInputDialog::getDouble(this, tr("Import floating point number dialog box"),tr("Please enter-1000 Values between and 1000"), 0.00, -1000, 1000, 2, &ok); if(ok) qDebug() << "value2:" << value2; // Get entry QString item = QInputDialog::getItem(this, tr("Input entry dialog box"),tr("Please select or enter an entry"), items, 0, true, &ok); if(ok) qDebug() << "item:" << item;
A total of four different types of input dialog boxes are created here.
- The getText() function can provide a dialog box for inputting strings. The parameters are used to specify the parent window, set the window title, set the display text of the label in the dialog box, set the display mode of the input string, set the default character string in the input box, and set the bool variable for obtaining the information of the pressed button.
- The getInt() function can provide a dialog box for entering integer values. The parameter 100 indicates that the default value is 100, - 1000 indicates that the minimum value that can be entered is - 1000, 1000 indicates that the maximum value that can be entered is 1000, and 10 indicates that the arrow button is used, and the value changes by 10 each time.
- The getDouble() function can provide a dialog box for entering a floating-point value, in which the parameter 2 indicates that the number of decimal places is 2.
- The getltem() function provides a dialog box where you can enter an item. You need to provide it with some items first, such as items of QStringList type defined here. Its parameter 0 indicates the 0th entry in the default display list; Then there is the parameter true, which sets whether the entry can be changed. True means that it can be changed.
5, Message dialog box
The message dialog box QMessageBox class provides a modal dialog box to notify the user of some information, or ask the user a question and get the answer.
// Question dialog int ret1 = QMessageBox::question(this, tr("Question dialog"),tr("You know Qt Are you?"), QMessageBox::Yes, QMessageBox::No); if(ret1 == QMessageBox::Yes) qDebug() << tr("Question!"); // Prompt dialog box int ret2 = QMessageBox::information(this, tr("Prompt dialog box"),tr("This is Qt Books!"), QMessageBox::Ok); if(ret2 == QMessageBox::Ok) qDebug() << tr("Tips!"); // Warning dialog box int ret3 = QMessageBox::warning(this, tr("Warning dialog box"),tr("Can't end early!"), QMessageBox::Abort); if(ret3 == QMessageBox::Abort) qDebug() << tr("Warning!"); // Error dialog int ret4 = QMessageBox::critical(this, tr("Critical error dialog box"),tr("A serious error was found! Close all files now!"), QMessageBox::YesAll); if(ret4 == QMessageBox::YesAll) qDebug() << tr("error"); // About dialog box QMessageBox::about(this, tr("About dialog box"),tr("yafeilinux betake Qt and Qt Creator Our popularization work!"));
Four different types of message dialog boxes are created here, with different icons and prompt tones (this is set by the operating system). The parameters are the parent window, title bar, display information and owned buttons. The buttons used here are standard buttons provided by the QMessageBox class. You can judge which button the user pressed according to the return value.
The about() function does not return a value because it has only one button by default. Similarly, there is an {about Qt() function to display the current Qt version and other related information. If you want to use your own icons and custom buttons, you can create a QMessageBox class object and then use related functions to operate.
6, Progress dialog box
The progress dialog qpprogressdialog provides feedback on the progress of a time-consuming operation.
QProgressDialog dialog(tr("File copy progress"), tr("cancel"), 0, 50000, this); dialog.setWindowTitle(tr("Progress dialog box")); // Set window title dialog.setWindowModality(Qt::WindowModal); // Set dialog box to modal dialog.show(); // Demonstrate replication progress for(int i=0; i<50000; i++) { dialog.setValue(i); // Sets the current value of the progress bar QCoreApplication::processEvents(); // Avoid interface freezing if(dialog.wasCanceled()) // Press the Cancel button to interrupt break; } dialog.setValue(50000); // In this way, 100% can be displayed, because a number is added less in the for loop qDebug() << tr("Copy finished!");
A qpprogressdialog class object dialog is created here. The parameters of the constructor are the label content of the dialog box, the display text of the Cancel button, the minimum value, the maximum value and the parent window. Then set it as a modal dialog box and display it.
The following for() loop statement simulates the file copy process, and uses the {setValue() function to move the progress bar forward. In order to avoid freezing the user interface due to long-time operations, you must constantly call the static function of QCoreApplication class, {processEvents(), which can be placed in the for() loop statement.
Then, use the {wascancelled() function of qpprogressdialog to determine whether the user has clicked the Cancel button. If so, the replication process will be interrupted. The modal dialog box is used here. In fact, qpprogressdialog can also implement non modal dialog boxes, but it needs the help of timers and so on.
7, Error message dialog box
The QErrorMessage class provides a dialog box that displays error messages.
QErrorMessage *errordlg = new QErrorMessage(this);; errordlg->setWindowTitle(tr("Error message dialog box")); errordlg->showMessage(tr("Here is the error message!"));
A new QErrorMessage dialog box is created here, and its showMessage() function is called to display the error message. When this function is called, the dialog box will be displayed in a non modal form.
8, Wizard dialog box
The QWizard class of the Wizard dialog box provides a framework for designing the wizard interface. Readers should be familiar with the Wizard dialog box, such as the wizard when installing software and the wizard when creating a project. QWizard is called a framework because it has all the functions of designing a wizard. You can use it to achieve the desired effect. There are three sample programs under the dialog category in the Qt demo program, namely, Trivial Wizard, License Wizard and Class Wizard, which can be used for reference.
Here, three functions whose return value is the pointer of QWizardPage class object are defined to generate three wizard pages:
QWizardPage * MyWidget::createPage1() // Wizard page 1 { QWizardPage *page = new QWizardPage; page->setTitle(tr("introduce")); return page; } QWizardPage * MyWidget::createPage2() // Wizard page 2 { QWizardPage *page = new QWizardPage; page->setTitle(tr("User selection information")); return page; } QWizardPage * MyWidget::createPage3() // Wizard page 3 { QWizardPage *page = new QWizardPage; page->setTitle(tr("end")); return page; } // wizard dialogs void MyWidget::on_pushButton_clicked() { QWizard wizard(this); wizard.setWindowTitle(tr("wizard dialogs ")); wizard.addPage(createPage1()); // Add wizard page wizard.addPage(createPage2()); wizard.addPage(createPage3()); wizard.exec(); }
Here, a new QWizard class object is created, and then three pages are added for it by using the {addPage() function. The parameter here is a pointer of QWizardPage type, so the function of generating wizard pages is directly used. Running the program, you can see that the order of wizard pages is the same as that of adding wizard pages.
Here, we have basically finished several standard dialog boxes provided by Qt. In fact, there are three standard dialog boxes related to printing, QPageSetupDialog (Page Setup dialog box), QPrintDialog (Print dialog box) and QPrintPreviewDialog (print preview dialog box). For the use of these dialog boxes, Qt provides a sample program Standard Dialogs, which is in the categories of Dialogs.