Mymainwindow.cpp
#include "mymainwindow.h" #include "ui_mymainwindow.h"//#include <QMenuBar> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//uses a forward declaration that does not use the form contained in the header file, mainly for faster compilation class qmenubar;// The forward declaration of the class Mymainwindow::mymainwindow (Qwidget *parent) : qmainwindow (parent), ui (New ui::mymainwindow) { ui->setupui (this); openaction = new qaction (tr ("&open"), this); Openaction->setshortcut (Qkeysequence::open);//shortcut keys for actions, such as: Open shortcut ctrl+o (qt qkeysequence definition) openaction->setstatustip (tr ("Open a file"));//Prompt statement to add status bar file = menubar ()->addmenu (tr ("&file")); file- >addaction (openaction), toolbar = addtoolbar (tr ("&File")); toolbar->addaction (Openaction);} Mymainwindow::~mymainwindow () { delete ui;}
After running,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8C/D5/wKiom1h6EqXxmWnoAAAnbu16hgU394.png-wh_500x0-wm_ 3-wmp_4-s_633353334.png "title=" Qq20170114195754.png "alt=" Wkiom1h6eqxxmwnoaaanbu16hgu394.png-wh_50 "/>
Add icon: 1, the first to use QT resource files. Right-click on the Qtcreator project, select New File ..., and then select Resource file.
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8C/D2/wKioL1h6H6aSK7zHAABVikmjAno610.png-wh_500x0-wm_ 3-wmp_4-s_3987849267.png "title=" Qq20170114205324.png "alt=" Wkiol1h6h6ask7zhaabvikmjano610.png-wh_50 "/>
2, then click Choose, Name, choose a good location, next, complete can. For ease of use, I will put this file in the root directory, it is recommended that after careful planning of the file, built in a dedicated rsources folder. When finished, a. qrc file is generated, and QRC is actually the abbreviation for QT Recource collection.
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8C/D2/wKioL1h6IELhJFYMAABNalFeFNs536.png-wh_500x0-wm_ 3-wmp_4-s_3230827717.png "title=" Qq20170114205608.png "alt=" Wkiol1h6ielhjfymaabnalfefns536.png-wh_50 "/>
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8C/D2/wKioL1h6IK7yutgNAAC6QEx1_Js344.png-wh_500x0-wm_ 3-wmp_4-s_3680204507.png "title=" Qq20170114205751.png "alt=" Wkiol1h6ik7yutgnaac6qex1_js344.png-wh_50 "/>
3, Click the Add button, first select Add prefix, and then change the generated/new/prefix to/. This is prefix is the prefix that you need to provide when you use the icon later, starting with/. After you have added prefix, add an icon to the project file, select Add File, and select the icon. Save the QRC file after this is complete.
650) this.width=650; "Src=" Https://s5.51cto.com/wyfs02/M00/8C/D6/wKiom1h6IQ7hZDJdAACnGkdBSlo301.png-wh_500x0-wm _3-wmp_4-s_3563571660.png "title=" Qq20170114205924.png "alt=" Wkiom1h6iq7hzdjdaacngkdbslo301.png-wh_50 "/>
How do you see the path of the picture? As you can see, the resource File view of QT uses the tree structure, the root is/, the leaf node is the picture position, joins together is the path. The path to this picture, for example, is/resource/open.png.
4, add code
class qicon; //class ... mymainwindow:: Mymainwindow (Qwidget *parent) : qmainwindow (parent), UI (New ui::mymainwindow) { ui->setupui (this); openaction = new qaction (tr ("&open"), this); openaction->setshortcut ( Qkeysequence::open);//shortcut keys for actions, for example: Open shortcut ctrl+o (qkeysequence definition of QT) openAction-> Setstatustip (tr ("Open a file"));//Add a prompt statement for the status bar openaction- >seticon (Qicon (":/resource/open.png"));//Add this code and add an icon file = menubar ()->addmenu (tr ("&file")); file->addaction (openAction); toolbar = addtoolbar (tr ("&file")); toolbar-> Addaction (openaction);}
Run 650) this.width=650; "Src=" Https://s5.51cto.com/wyfs02/M02/8C/D6/wKiom1h6IlWjwdg8AAAfbhcMaTs074.png-wh_ 500x0-wm_3-wmp_4-s_526749450.png "title=" Qq20170114210457.png "alt=" wkiom1h6ilwjwdg8aaafbhcmats074.png-wh_50 "/ >
This article is from the "11907685" blog, please be sure to keep this source http://11917685.blog.51cto.com/11907685/1892014
Subclass Qmainwindows, add menu bars and toolbars, and add icons