I have written 15th articles on the path to Qt learning, but it is a little difficult to write it again because I didn't expect it to be written continuously at the beginning, therefore, there is no good plan for writing such content. Although I mentioned earlier, the main line of this tutorial is referred to "C ++ Gui Programming with Qt 4, 2nd Edition". However, the latest chapter has changed because the original article is a relatively complete project, therefore, we do not know where to start. I am not going to introduce the use of many components, because Qt has many components and there are a lot of Components in use. It is impossible to finish the introduction. I can only put the API at hand and check it while using it. Therefore, I will give a brief introduction to many components. Please search for the specific usage (specifically, I do not know much about it, in most cases, you still need to go to the API ). Next we will start from the standard Qt Dialog Box Based on our progress. The so-called standard dialog box is actually some built-in Qt dialogs, such as file selection and color selection. Today, we will first introduce QFileDialog. QFileDialog is a dialog box used to open and save files in Qt, which is equivalent to JFileChooser in Swing. Open the project we used earlier. We have already written an open action with foresight. Do you still remember the previous code? At that time, we only popped up a message dialog box (this is also a standard dialog box ~) It is used to inform that the signal slot has been connected. Now we need to publish the code! Modify the open function of MainWindow: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> void MainWindow: open ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> QString path = QFileDialog: getOpenFileName (this, tr ("Open Image "),". ", tr (" Image Files (*. jpg *. png )"));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> if (path. length () = 0 ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> QMessageBox: information (NULL, tr ("Path"), tr ("You didn't select any files. "));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> QMessageBox: information (NULL, tr ("Path"), tr ("You selected") + path );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/>} Do not forget to include QFileDialog before compiling! Run it! Click the OPEN button to bring up the open dialog box. Select a file or click Cancel directly. A message is displayed. QFileDialog provides many static functions for obtaining the selected files. Here we use getOpenFileName (), that is, "Get open file name". You can also view the API to find more functions. However, the parameter of this function is quite long and the type is QString, which is not easy to remember. Given this situation, Qt provides another Syntax: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> QFileDialog * fileDialog = new QFileDialog (this );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> fileDialog-> setWindowTitle (tr (" Open Image "));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> fileDialog-> setDirectory (".");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> filediign-> setFilter (tr ("Image Files (*. jpg *. png )"));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> if (fileDialog-> exec () = QDialog: Accepted ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> QString path = fileDialog-> selectedFiles () [0];
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> QMessageBox: information (NULL, tr ("Path"), tr ("You selected") + path );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif"/> QMessageBox: information (NULL, tr ("Path"), tr ("You didn't select any files. "));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/>} However, although the two methods have little difference in functionality, the pop-up dialog box is not the same. The getOpenFileName () function provides a local dialog box on the Windows and MacOS X platforms, while the QFileDialog provides a self-drawn dialog box by Qt (as mentioned earlier, like Swing, Qt components are also self-drawn, rather than calling system resource APIs ). To demonstrate the usage of the QFileDialog: getOpenFileName () function, put the function signature here: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S6221024-0.gif "/> 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 parameter parent, used to specify the parent component. Note that many Qt component constructors have such a parent parameter and provide a default value of 0. The second parameter is the title of the dialog box. The third parameter is dir, is the default directory opened when the dialog box is displayed ,". "indicates the program running directory,"/"indicates the root directory of the current drive letter (Windows, Linux/is the root directory), or platform-related, such as" C: \ ", etc.; the fourth parameter is filter, which is the suffix filter of the dialog box. For example, we use" Image Files (*. jpg *. png. To use multiple filters, use ";" to separate them, for example, "JPEG Files (*. jpg); PNG Files (*. png) "; the fifth parameter, selectedFilter, is the default filter. The sixth parameter, options, is some parameter settings in the dialog box, such as displaying only folders. Its value is enum QFileDialog:: Option. Each Option can be combined with | operations. What if I want to select multiple files? Qt provides the getOpenFileNames () function, whose return value is a QStringList. You can understand it as a List that can only store QString, that is, the list in STL <string>. Now, we can choose to open the file. Similarly, the QFileDialog class also provides the getSaveFileName function for saving the dialog box. For more information, see the API.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/213414