Qt learning notes - QFileDialog of 5-Qt standard dialog box

Posted by agrant on Sat, 15 Jan 2022 16:07:29 +0100

The so-called standard dialog box is actually some dialog boxes built in Qt, such as file selection, color selection, etc.

QFileDialog is a dialog used to open and save files in Qt.

The previously written openAction just pops up a message dialog box to tell you that the signal slot has been connected. Now you need to write the real opening code!

Modify the open function of MainWindow:

Writing method 1

//#include <QFileDialog>
void MainWindow::open() 
{ 
        QString path = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files(*.jpg *.png)")); 
        if(path.length() == 0) { 
                QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files.")); 
        } else { 
                QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path); 
        } 
}

include QFileDialog before compiling! Then run it! Click the open button, the open dialog box will pop up, and then select a file or click Cancel directly, and there will be a corresponding message prompt.

Operation effect


Writing method 2

QFileDialog provides many static functions to obtain files selected by users. Here we use getOpenFileName(), that is, "get open file name". However, the parameters of this function are quite long, and the types are QString, which is not easy to remember. Considering this situation, Qt provides another way to write:

QFileDialog *fileDialog = new QFileDialog(this); 
        fileDialog->setWindowTitle(tr("Open Image")); 
        fileDialog->setDirectory("."); 
        fileDialog->setFilter(tr("Image Files(*.jpg *.png)")); 

        if(fileDialog->exec() == QDialog::Accepted) { 
                QString path = fileDialog->selectedFiles()[0]; 
                QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path); 
        } else { 
                QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files.")); 
        }

However, although the functions of the two writing methods are not very different, the pop-up dialog boxes are not the same. The getOpenFileName() function provides a local dialog box on Windows and MacOS X platforms, while the QFileDialog always provides a dialog box drawn by Qt itself (remember that the components of Qt are drawn by Qt, similar to Swing, rather than calling the system resource API).

Usage of getOpenFileName() function

To illustrate the usage of the QFileDialog::getOpenFileName() function, first place the function declaration here:

QString QFileDialog::getOpenFileName (
          QWidget * parent = 0, //Used to specify the parent component. Note that Qt component constructors have a parent parameter and provide a default value of 0;
          const QString & caption = QString(),//Title of the dialog box
          const QString & dir = QString(),//The default directory opened when the dialog box is displayed is "." Represents the program running directory "/" represents the root directory of the drive letter
          const QString & filter = QString(),//Dialog box suffix filter
          QString * selectedFilter = 0,//Filter selected by default
          Options options = 0 )//Some parameter settings of the dialog box, such as displaying only folders, etc

The first parameter, parent, specifies the parent component. Note that the constructor of many Qt components will have such a parent parameter and provide a default value of 0;

The second parameter, caption, is the title of the dialog box;

The third parameter dir is the directory opened by default when the dialog box is displayed, "." Represents the program running directory, "/" represents the root directory of the current drive letter (under Windows and Linux / is the root directory), or it can be platform related, such as "C:";

The fourth parameter filter is the suffix filter of the dialog box. For example, if we use "image files (. jpg. png)", it can only display files with the suffix jpg or png. If multiple filters need to be used, use ";;" split, such as "JPEG Files(.jpg);;PNG Files(.png)";

The fifth parameter, selectedFilter, is the filter selected by default;

The sixth parameter options is some parameter settings of the dialog box, such as displaying only folders. Its value is enum QFileDialog::Option. Each option can be combined by | operation.

Select multiple files

What if I want to select multiple files? Qt provides the getOpenFileNames() function, and its return value is a QStringList. You can understand it as a list that can only store QString, that is, the list in STL.

Well, we have the option to open the file. Saving is similar. The QFileDialog class also provides the function getSaveFileName to save the dialog

Topics: Qt Visual Studio