QT Menu item Customization

Source: Internet
Author: User

QT Implementation menu, simple interface qmenu+qaction completely can be implemented, in addition to QSS support, you can customize a more beautiful menu, QT menu is generally used in trays, buttons and toolbars.

Of course, there are a lot of software has more beautiful tray menu, such as 360, computer Butler and other software, 1, in fact, QT after 4.2 also provides a custom menu function, using Qwidgetaction can customize the menu you want, followed by my custom menu bar steps.

Figure 1 360 Chart Tray Menu

Implementation effect as shown in 2, the menu is composed of a single item, each entry is made up of two parts, the left is an icon, with a background, the right is a label, the top has a text description, when the mouse moved to the item, the whole background color of the item becomes red, and the icon is replaced, The text color will also change accordingly.

Figure 2 Customizing the menu

First get this function, we can first consider the function of the split, since QT Support menu item window customization function, then we do not have to customize each project into a qwidget, so that the problem becomes a window of customization, so it seems not much simpler.

First we look at the Qsystemtrayicon class, which implements the function of the Windows tray, the activated signal indicates that the tray icon has an event, we need to process this signal, when the messageclicked signal is triggered, we click on the tray tip information. Here is the pallet class I rewrote

1 classCsystemtrayicon: PublicQsystemtrayicon2 {3 Q_object4 5 Signals:6     voidshowmainwidget ();7     voidshowminiwidget ();8     voidappquit ();9 Ten  Public: OneCsystemtrayicon (ConstQicon & Icon, Qobject * parent =nullptr); A~Csystemtrayicon (); -  -  Public: the     voidSetwaverable (BOOLWaver);//whether the tray icon is flashing -     voidShowMessage (ConstQString &title -,ConstQString &message -, Qsystemtrayicon::messageicon icon =qsystemtrayicon::information +,intMillisecondstimeouthint = the);//Tray Popup Bubble Tip -  +Qaction * Addaction (ConstQString & Actname,ConstQicon &icon); A  at protected: -     Virtual BOOL Event(Qevent *) Q_decl_override; -     Virtual voidTimerEvent (Qtimerevent *) Q_decl_override; -     Virtual BOOLEventFilter (Qobject * watched, qevent *Event) Q_decl_override; -  - PrivateSlots: in     voidTrayactivateslot (Qsystemtrayicon::activationreason); -     voidMessageclickedslot (); to  + Private: -     voidCreateMenu (); the  * Private: $     BOOLM_mouseleave =true;Panax Notoginseng     intM_timeid =0; - Qicon M_iconpath; theCtraymenu * M_menu =nullptr; + #ifdef CustomAction A     //Add a custom menu item theCactionitem * Mainact =nullptr; +Cactionitem * Miniact =nullptr; -Cactionitem * Exitact =nullptr; $ #else $Qaction * Mainact =nullptr; -Qaction * Miniact =nullptr; -Qaction * Exitact =nullptr; the #endif -};
View Code

By looking at the interface of the Csystemtrayicon class, you can see that there are other interfaces in the class, but I do not intend to explain them in this article, because it is not related to the menu item customization, if you want to ask, I can only answer the instructions under the order, Setwaverable interface use class to set the tray icon is flashing, similar to the effect of QQ.

Next we need to understand the next Qwidgetaction class, which inherits from Qaction, he has all the signals and slots of qaction, so we can use him as a qaction, not only that, we can also provide them with custom qwidget, The implementation code is as follows

1 classCactionitem: Publicqwidgetaction2 {3 Q_object4Q_property (BOOLm_hover READ ismhover WRITE setmhover)5 6  Public:7Cactionitem (ConstQString & text ="", Qwidget * parent =nullptr);8~Cactionitem ();9 Ten  Public: One     voidSetcontenttext (ConstQString &text); A     voidSetitemicon (ConstQString &icon); -     voidSetitemicon (ConstQString & Icon,ConstQString &hover); -     voidSetitemicon (ConstQString & Icon,ConstQString & Hover,ConstQString &Press ); the  -Qwidget * Contentwidget ()Const;//Get Center window -     voidSetToolTip (ConstQString &toolTip); -  +  Public: -     BOOLIsmhover () {returnM_hover;} +     voidSetmhover (BOOLHover) {M_hover =hover;} A  at protected: -     VirtualQwidget * Createwidget (Qwidget *parent) Q_decl_override; -     Virtual voidDeletewidget (Qwidget *widget) Q_decl_override; -      - Private: -     BOOLM_hover =false; inCactioncontentwidget * M_contentwidget =nullptr; -};
View Code

When a qwidgetaction is created, we first initialize our custom window in the constructor and set it as the default window

Cactionitem::cactionitem (const QString & Text, Qwidget * parent/*= nullptr*/): qwidgetaction (parent) {setenabled ( true); m_contentwidget = new Cactioncontentwidget (); Connect (M_contentwidget, &cactioncontentwidget::iconclicked , this, [This]{this->triggered ();}); M_contentwidget->setcontenttext (text); Setdefaultwidget (m_contentwidget);}

The Createwidget interface is automatically called, so we can create our own custom Qwidget in this interface. The code below, remember to set the custom window as parameters to the window of the child window

Qwidget * Cactionitem::createwidget (Qwidget * parent) {m_contentwidget->setparent (parent); return m_contentwidget;}

Qwidgetactoin is just a qaction, want to beautiful menu items, or need us to customize the window, next is my own custom window

1 classCactioncontentwidget: PublicQwidget2 {3 Q_object4 Signals:5     voidiconclicked ();6 7  Public:8Cactioncontentwidget (qwidget * parent =nullptr);9~cactioncontentwidget ();Ten  One  Public: A     voidSetcontenttext (ConstQString &text); -     voidSetitemicon (ConstQString & Icon,ConstQString &hover); -  the  Public: -     voidSetbackgroundrole (BOOLhover); -  - protected: +     Virtual voidEnterevent (Qevent *) Q_decl_override; -     Virtual voidLeaveevent (Qevent *) Q_decl_override; +     Virtual BOOLEventFilter (Qobject *, qevent *) Q_decl_override; A  at Private: -     voidInitializeui (); -  - Private: -Qwidget * M_contentwidget =nullptr; -Qpushbutton * M_acticon =nullptr; inQlabel * M_acttext =nullptr; - QString M_normalicon, M_hovericon, M_pressedicon; to};
View Code

Finally, the menu is customized, why to rewrite the menu, because I need to change the location of menus at the specified time, so the customization of the menu item is relatively simple, is to run a signal at the critical moment, indicating the need to modify the menu position, the code is as follows:

1 classCtraymenu: PublicQmenu2 {3 Q_object4 5 Signals:6     voidFixedpostion ();//Move Menu Position7 8  Public:9Ctraymenu (qwidget * parent =nullptr);Ten~Ctraymenu (); One  A protected: -     Virtual BOOL Event(Qevent *) Q_decl_override; -};
View Code

Because the menu is a qwidget, the width and height are not available in the constructor, and you can get the relevant information at show, the code is as follows:

BOOL Ctraymenu::event (Qevent * e) {if (e->type () = = Qevent::show) {emit fixedpostion ();} Return Qmenu::event (e);}

Here, QT menu customization function is finished, in the process of menu customization I also encountered some problems, in this record, want to see and know the reason for leaving your footprints.

Problem:

1, custom Qwidget in the mouse event exception

2, QSs in the property to judge the exception, such as Qlabel[ischeck=true]{border:1 solid #ff0000;}, the mouse changes set in this way does not work, in order to achieve this function, I was in the Csystemtrayicon class to register the custom window events in the parent class, and then through the eventfilter to determine the mouse position, and further reset the QSS to reach the mouse to move the background color function. The code is as follows:

BOOL Csystemtrayicon::eventfilter (Qobject * watched, Qevent * event) {if (watched = = this) {M_mouseleave = false;} if (Watched->inherits ("Qwidget") && event->type () = = Qevent::P aint) {if (Cactioncontentwidget * Actionitem = Static_cast<cactioncontentwidget *> (watched)) {if (Actionitem->rect (). Contains (actionItem- >mapfromglobal (qcursor::p OS ()))) {Actionitem->setbackgroundrole (true);} Else{actionitem->setbackgroundrole (FALSE);}}} Return Qsystemtrayicon::eventfilter (watched, event);}

QT Menu item Customization

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.