How to customize menus in QT

Source: Internet
Author: User

 

The style of a menu is determined by the menu items above it. Therefore, to customize a menu, you need to customize each menu item. In QT, the menu item is a qaction class, but if you use it directly: its style is determined by the system. We can only write some characters above or add an icon.

However, sometimes we need to customize a personalized menu. For example, the overall style of your app is blue, but the system menu item on QT for Symbian is black, and QT for Windows is gray. Obviously, this is not the case, so we need to personalize it.

The simplest way to personalize a menu is to brush an image on the top of the menu. The expected results are as follows:

1: When the mouse hover moves into the menu area: it should be changed to another background image.

2: When the mouse hover moves out of this menu option: it restores the original background image.

3: When mousepress: change to another image.

4: When mouserelease is used, the original background image is replaced and the menu itself is hidden.

From the above analysis, we can see that at least two images must be prepared when a menu item is customized. One is used for the normal state, and the other is used for the hover-in and mouse-Press states.

Else ---------------------------------------------------------------------------------------------------------------------------------

For the standard menu item control qaction, since it does not inherit from qwidget, we can no longer draw the image above. We can only call its own functions to set an icon and write point characters, which obviously does not meet our requirements.

To customize menu items for personalized calls, QT provides another class: qwidgetaction.

That is:Common menu items can be usedQactionImplementation.We want to use customized menu itemsQwidgetaction.

 

The customization method is as follows: it can act as an intermediate "container" and put a common qwidget on it, in this way, the widget is displayed in front of the user. For this Widget: We can customize menu items as needed, so as to indirectly display customization of menu items.

 

From the above: our so-called personalized customization is to first create a qwidget object of the specified style, then place it on qwidgetaction, and finally put the created qwidgetaction object on the menu panel !!

Bytes ------------------------------------------------------------------------------------------------------------------------

So: As mentioned above, there is another problem to be solved: Who is the parent of the common widget above? We cannot specify it as qwidgetaction. After all, it does not inherit from qwidget and cannot be used as a parent.

In fact, the parent window of this normal window is directly designated as: the menu qmenu for storing this menu item. It is assumed that the qmenu is inherited from the qwidget and can be used as a parent.

Specific implementation:

①: For qwidgetaction, We need to derive and rewrite its member function:

Qwidget * createwidget (qwidget *);

This is explained in the help document:

This function is called whenever the action is added to a container widget that supports custom Widgets

That is, when we add a personalized menu item to the container window, this function is called immediately.

 

The function is used to set the parent window of the common widget on which the menu item is created as the container window (qmenu) where the menu item is located. The input parameters of this function are: the parent of the qwidgetaction, which is passed by the system, and we don't need to worry about it.

Bytes ---------------------------------------------------------------------------------------------------------------------------

 

The main process is as follows:

1: first derive qwidgetaction and create a custom common widget in it (note that this widget does not specify the parent object)

2: override the createwidget () function and specify its parent window as the container window where the menu item is located, that is, the passed pointer.


The next step is to rewrite the mouse event and hover event to implement the image transform mentioned above. I will not repeat it here.

(An example is uploaded to my resources. I have encapsulated the classes and can use them directly. If you are interested, you can download them and give me 1 point ~ You can also send me an email, I will definitely send you within 24 hours, e-mail: jason-dou@hotmail.com)

 

 

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.