QT Settings menu icon

Source: Internet
Author: User
Tags line editor

Catalogue First, add the main Window menuSecond, add the menu iconThird, add the resource fileIv. use of resource filesv. Use code to add menus and icons

Body

First, add the main Window menu

1.NewQt Guiapplication, the project name isMymainwindow, base class selectionQmainwindow, the class name isMainWindow.

2.When you finish creating the project, openMainwindow.uifile into design mode. Here you can see the "Enter here" in the upper-left corner of the interface, where we can add a menu. Double-click "Enter Here" to change it to "file(&f), and then press ENTER, as shown in the effect. Here's&findicates that the shortcut key for the menu is set toALT + F, you can see that the actual display effect&the symbol is hidden. <ignore_js_op>

3.In the same way, we add "new" in the File menu(&n)submenu, as shown in the effect. The plus icon behind the menu is used to create the next level menu. <ignore_js_op>

Second, add the menu icon

1.Qta menu is seen as aAction, we are in the followingActionyou can see the new menu you just added, as shown in the editor. <ignore_js_op>

2.double-clicking on the entry will bring up the Edit Action dialog, where you can set the settings, for example, we can set the menu shortcut keys, click the shortcut key after the line editor, and then press the keyboardCtrl + N, so you can set the shortcut key for the menu toCtrl + N. As shown in. So you might ask, since the shortcut for this menu is set, the "new" setting aboveN"in theNWhat is it? This can be called the accelerator key, which is only when the File menu is in the active (displayed) state, pressNbutton to perform the new menu function. <ignore_js_op>

3.in the Edit Action dialog box, after the icon<ignore_js_op>The black Arrow drop-down box lets you choose whether to use a resource or a file, and if you use a file, you can select an icon file on your local disk directly in the Pop-up file dialog box. Let's talk about how to use resources, and if clicking this button directly is the default use of resources. Now let's press the OK button of the Edit Action dialog box to close it.

Third, add the resource file

1.QtYou can use a resource file to add various types of files to the resulting executable file, which avoids some of the problems that may occur with external files. Also, at compile timeQtThe resource files are also compressed, and we may find that the resulting executables are smaller than the resource files that we add to them.

2.we add new files to the project, template selectionQtThe resource file. As shown in. Then set the name toMyResources.

<ignore_js_op>

3.After creating the file will automatically open the resource file, where you need to first add a prefix, that is, click the Add button, and then select the prefix, the default prefix is "/new/prefix1 ", this can be arbitrarily modified (do not appear Chinese characters), we here because to add a picture, so modify to/myimages. Then press the Add button to add the file, it's best to put all the pictures you want to use in the project directory. For example, we have created a new one in the project directoryImagesfolder, and then paste the desired icon file in. After you have finished adding the files, as shown in. <ignore_js_op>

4.when you are finished adding resources, be sure to pressCtrl + Sto save the resource file, or you may not be able to display the resources you have added later.

Iv. use of resource files

1.we go back to design mode and open the Edit Action dialog box for the new menu, and then add the icon. In the Select Resources dialog box that opens, you may not be able to display resources that already exist for the first time, and you can press the green arrow in the upper-left corner to update the display. The effect is as shown. <ignore_js_op>

2.We click on the new icon needed hereFilenew.png, press OK. Now press theCtrl + RKey to run the program, as shown in the effect. <ignore_js_op>

v. Use code to add menus and icons

1.for the added resource file, you can see in the project directory thatMyresources.qrc, using the WordPad program to open it, you can see that it is actually aXmlDocumentation:<RCC><qresourceprefix= "/myimages" ><file>images/filenew.png</file><file>images/fileopen.png</file><file>images/filesave.png</file><file>images/filesaveas.png</file><file>images/find.png</file>    </qresource></RCC>

2.above is the icon file added in design mode, below we use the code to add a menu and set the icon for it. Open in edit modeMainwindow.cppfile and add the following code to the constructor:

// Create a new actionqaction *openaction = new Qaction (tr ("&open"), this);// Add iconQicon icon (":/myimages/images/fileopen.png");Openaction->seticon (icon);// set shortcut keysopenaction->setshortcut (qkeysequence (tr ("Ctrl+o" ));// set a new open action in the File menuui->menu_f->addaction (openaction);

When you add an icon here, you are using the icon in the resource file. Using a resource file, you need to start with a colon and then add a prefix followed by the path to the file. Using the File menu in your code is to use itsObjectName. You can now run the program to see the effect, of course, it can also beOpenchange to Chinese.



Conclusion This article explains how to use resource files and describes two ways to use them in design patterns and code. I hope that you can practice the contents of this article, in the following chapters, for adding menus and icons and other actions will not be explained

QT Settings menu icon

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.