Summary of the QT open file dialog box, And the qt open file dialog box

Source: Internet
Author: User

Summary of the QT open file dialog box, And the qt open file dialog box
Qstring fileName = QFileDialog: getOpenFileName (this, tr ("open file"), "", tr ("Allfile (*. *); mp3file (*. mp3) "); Note: a dialog box is generated, which is similar to the system resource manager. The absolute path of the selected file is returned. Parameter 1: parent window
Parameter 2: The title of the dialog box
Parameter 3: Default open location, such as "My Documents"
Parameter 4: file filter. Note that the filter is used between file types; separated by QStringList fileNameList;
QString fileName0; QFileDialog * fd = new QFileDialog (this); // create dialog box
Fd-> resize (240,320); // you can specify the display size.
Fd-> setFilter ("Allfile (*. *); mp3file (*. mp3); wmafile (*. wma); wavefile (*. wav) "); // sets the file filter.
Fd-> setViewMode (QFileDialog: List); // sets the browsing mode. There are two methods: list mode and detail mode. if (fd-> exec () = QDialog: Accepted) // if the execution is successful
{
FileName0 = fd-> selectedFiles (); // name of the returned file list
FileName = fileNameList [0]; // obtain the first file name
}
Else
Fd-> close ();
The materials are from the official Qt documents. I have summarized some of the frequently used documents: 1. the simplest method is to call static functions to obtain files in the dialog box:
QString file = QFileDialog: getOpenFileName (
"/Home/foxman ",
"Images (*. png *. xpm *. jpg )",
This );
This code can be used to create a file fetch dialog box. After the selection, OK will return the file path to the file.

II. General file dialog box usage
QFileDialog * fd = new QFileDialog (this, "file dlg", TRUE );
If (fd-> exec () = QFileDialog: Accepted) // OK
{
QString file = fd-> selectedFile ();
QWarning (s );
} 1. Several settings: a. Set the Display Mode
Fd-> setViewMode (QFileDialog: Detail );
// Detail displays the detailed file date and size. List is normal. B. Set the filter.
Fd-> setFilter ("Images (*. png *. xpm *. jpg )");
Below are multiple filters, which must be separated by; (two semicolons)
QString filters = "C file (*. c *. cpp *. h); pic (*. png *. xpm )";
Fd-> setFilters (filters); c. Set what is returned in the dialog box
Fd-> setMode (QFileDialog: ExistingFile );
AnyFile (generally used in the save as dialog box)
ExistingFile
ExistingFiles contains 0 or more files (multiple files can be selected)
Directory
DirectoryOnly returns the directory (only the directory is selected when the file is selected)
 
2. Return Value:
A. Return the name of the selected file (folder ).
QString s = fd-> selectedFile ();
B. Select multiple files (you must set the ExistingFiles Mode)
QStringList slist = fd-> selectedFiles ();
For (QStringList: Iterator it = slist. begin (); it! = Slist. end (); it ++) // iterator
QWarning (* it );
Appendix: How to Use string list QStringList 1. You can add elements by append, +, or <
QStringList slist;
Slist. append ("string1 ");
Slist + = "string2 ";
Slist <"string3" <"string4"; 2. the iterator obtains the value of each variable.
For (QStringList: Iterator it = slist. begin (); it! = Slist. end (); it ++) qWarning (* it); 3. A usage method
QString s = "Red \ tGreen \ tBlue ";
QStringList colors = QStringList: split ("\ t", s );
Cout <colors. join (",") <endl;
Output: Red, Green, and Blue Qt file opening dialog box

1. Open a single file

QString filename = QFileDialog: getOpenFileName (
This,
"Open Document ",
QDir: currentPath (),
"Document files (*. doc *. rtf); All files (*.*)");
If (! Filename. isNull () {// file selected
// Process files
QMessageBox: information (this, "Document", "Has document", QMessageBox: OK | QMessageBox: Cancel );
} Else // cancel the selection
QMessageBox: information (this, "Document", "No document", QMessageBox: OK | QMessageBox: Cancel );

2. You can select the file opening dialog box for multiple files.

QFileDialog: Options options;
If (! Native-> isChecked ())
Options | = QFileDialog: DontUseNativeDialog;
QString selectedFilter;
QStringList files = QFileDialog: getOpenFileNames (
This, tr ("QFileDialog: getOpenFileNames ()"),
OpenFilesPath,
Tr ("All Files (*); Text Files (*. txt )"),
& SelectedFilter,
Options );
If (files. count ()){
OpenFilesPath = files [0];
OpenFileNamesLabel-> setText (QString ("[% 1]"). arg (files. join (",")));
}

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.