In the previous section, we have added QAction to the menu and toolbar. Now we need to add some images to beautify the image, and then add the signal slot so that our actions can be matched! First, add the icon. The QAction icon is displayed in front of the menu item and on the toolbar button. To add icons, we must first use the Qt resource file. Right-click the QtCreator project, select New File..., and then select resource file. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ94Y1-0.png "/> Click next, select the location, and Finish. For ease of use, I will create this file under the root directory. We recommend that you carefully plan the file and create it under the dedicated rsources folder. After that, A. qrc file is generated. qrc is short for Qt Recource Collection. It is just a common XML file and can be opened in notepad. However, here we will not go into its structure, and use QtCreator to operate this file completely, 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ962Q-1.png "/> Click the Add button. First, select Add prefix and change the generated/new/prefix /. This is the prefix that needs to be provided when you use the icon in the future, starting. After the prefix is added, Add an icon to the project file, select Add file, and select the icon. Save the qrc file. The default value of the QToolBar icon is 32*32, and the default value of the menu is 16*16. If the size of the provided icon is smaller than the required size, do not perform this operation. Qt will not enlarge the image for you. Otherwise, if the provided icon file is larger than the required size, for example, 64*64, qt automatically reduces the size. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ95M3-2.png "/> What is the picture path? We can see that the resource file view of Qt uses a tree structure. The root is/, the leaf node is the image location, and the link is the path. For example, the path of this image is/Open.png. NOTE: For the sake of simplicity, we did not place the icon in a special folder. In a formal project, there should be a separate resource folder for storing resource files. Return to mainwindow. cpp and modify the code in the constructor: 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/1SJ92S8-3.gif "/> OpenAction = new QAction (tr (" & Open "), 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/1SJ92S8-3.gif "/> openAction-> setShortcut (QKeySequence: Open );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ92S8-3.gif "/> openAction-> setStatusTip (tr (" Open a file ."));
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/1SJ92S8-3.gif "/> OpenAction-> setIcon (QIcon (":/Open.png "); // Add code. We use setIcon to Add the icon. The added class is QIcon. The constructor requires a parameter, which is a string. Because we want to use the image defined in qrc, the string starts with: followed by prefix. Because the prefix we previously defined is/, we need /, the file path is followed. This is defined in the preceding qrc. Open qrc and check the path of the image. Now, the image is added, and click run to see the effect! 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ92N5-7.png "/> Look! We only need to modify the QAction, And the menu and toolbar have been properly processed for us, which is very convenient! Next, add Event Response for QAction. Do you still remember that Qt's event response mechanism is based on the signal slot? Clicking QAction will send a triggered () signal, so what we need to do is to name a slot and connect the signal. Mainwindow. h 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/1SJ92S8-3.gif "/> Class MainWindow: public QMainWindow
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/1SJ92S8-3.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/1SJ92S8-3.gif "/> Q_OBJECT
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/1SJ92S8-3.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/1SJ92S8-3.gif "/> public:
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/1SJ92S8-3.gif "/> MainWindow (QWidget * parent = 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/1SJ92S8-3.gif "/> ~ 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/1SJ92S8-3.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/1SJ92S8-3.gif "/> private slots:
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/1SJ92S8-3.gif "/> void 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/1SJ92S8-3.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/1SJ92S8-3.gif "/> private:
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/1SJ92S8-3.gif "/> QAction * openAction;
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/1SJ92S8-3.gif"/>}; because our open () is currently used only inside the class, it is defined as private slots. Then modify the cpp file: 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/1SJ92S8-3.gif "/> MainWindow: MainWindow (QWidget * parent)
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/1SJ92S8-3.gif "/>: QMainWindow (parent)
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/1SJ92S8-3.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/1SJ92S8-3.gif "/> openAction = new QAction (tr (" & Open "), 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/1SJ92S8-3.gif "/> openAction-> setShortcut (QKeySequence: Open );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ92S8-3.gif "/> openAction-> setStatusTip (tr (" Open a file ."));
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/1SJ92S8-3.gif "/> openAction-> setIcon (QIcon (":/Open.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/1SJ92S8-3.gif"/> connect (openAction, SIGNAL (triggered (), this, SLOT (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/1SJ92S8-3.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/1SJ92S8-3.gif"/> QMenu * file = menuBar ()-> addMenu (tr ("& File "));
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/1SJ92S8-3.gif "/> file-> addAction (openAction );
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/1SJ92S8-3.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/1SJ92S8-3.gif "/> QToolBar * toolBar = addToolBar (tr (" & File "));
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/1SJ92S8-3.gif "/> toolBar-> addAction (openAction );
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/1SJ92S8-3.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/1SJ92S8-3.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/1SJ92S8-3.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/1SJ92S8-3.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/1SJ92S8-3.gif"/> QMessageBox: information (NULL, tr ("Open"), tr ("Open a file "));
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/1SJ92S8-3.gif"/>} note that a standard dialog box is displayed in the open () function without any other operations. Run after compilation to see the effect: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1SJ9D57-42.png "/> okay, the QAction has been added! So far, QAction problems have come to an end. Finally, if you do not know how to add a sub-menu, check the QMenu API, which contains an addMenu function. That is to say, you can create a QMenu and add it!
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/205958