QT Learning Pathway (qfiledialog): QT Standard dialog box

Source: Internet
Author: User

The following is our progress, starting with the Qt Standard dialog box. The so-called Standard dialog box, in fact, is a number of QT built-in dialog boxes, such as file selection, color selection and so on. Today I'd first introduce Qfiledialog.

Qfiledialog is a dialog box for file opening and saving in Qt, which is equivalent to the JFileChooser in swing. Below open the project we used earlier. We have already had the foresight to write an open action, remember the previous code? At that time, we just popped up a message dialog box (this is also a standard dialog box OH) to tell the signal slot has been unicom, now we have to write the real open code!

To modify the MainWindow open function:

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);
 }
}

Don't forget to include qfiledialog before compiling! Then run it! Click on the Open button, will pop open the dialog box, and then select the file or directly click Cancel, there will be a corresponding message prompts.

Qfiledialog provides a number of static functions to get the files selected by the user. Here we are using GetOpenFileName (), which is "Get open file name", you can also see the API to find more functions to use. However, the parameter of this function is quite long, and the type is all qstring, it is not easy to remember. With this in mind, QT provides a different wording:

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, these two kinds of writing, although the function is not very different, but the pop-up dialog box is not the same. The GetOpenFileName () function provides a local dialog box on Windows and MacOS X platforms, and Qfiledialog always provides a dialog box that Qt itself draws (remember, as I said earlier, that Qt's components are similar to swing and are drawn by themselves). Instead of calling the system resource APIs.

To illustrate the use of the Qfiledialog::getopenfilename () function, first put the function signature here:

QString QFileDialog::getOpenFileName (
 QWidget * parent = 0,
 const QString & caption = QString(),
 const QString & dir = QString(),
 const QString & filter = QString(),
 QString * selectedFilter = 0,
 Options options = 0 )

The first argument parent, which specifies the parent component. Note that many of the QT component constructors have such a parent parameter and provide a default value of 0;

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

The third parameter, dir, is the default open Directory when the dialog box is displayed, "." Represents the program run directory, "/" is the root of the current drive (Windows,linux/is the root directory), can also be platform-related, such as "c:\\";

The fourth parameter, filter, is the suffix name Filter for the dialog box, for example, we use "Image files (*.jpg *.png)" To let it show only the suffix name is jpg or PNG file. If you need to use multiple filters, use the ";;" segmentation, such as "JPEG Files (*.jpg);; PNG Files (*.png) ";

The fifth parameter Selectedfilter, is the default selection filter;

The sixth parameter options, is a dialog box some parameter settings, such as only display folders, and so on, its value is enum qfiledialog::option, each option can be used | Operations together.

What if I want to select multiple files? QT provides the getopenfilenames () function, whose return value is a qstringlist. You can think of it as a list that can only store qstring, that is, list<string> in STL.

Well, we've been able to choose to open the file. Saving is similar, the Qfiledialog class also provides a function to save the dialog box GetSaveFileName, use or consult the API.

Source: http://devbean.blog.51cto.com/448512/213414

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.