Qpushbutton drop-down menu (whether to cancel the drop-down triangle, check)

Source: Internet
Author: User

To add a menu to Qpushbutton, there are three ways ahead:

QT Learning to Qpushbutton add menu Actionscontextmenu method

QT Learning to Qpushbutton Add menu Customcontextmenu method

QT Learning to Qpushbutton add menu Defaultcontextmenu method

One more way today is to add a drop-down menu to Qpushbutton. Similar to a ComboBox. The first three ways to do this are where the cursor is located and where the menu is displayed. The drop-down menu is displayed only below the button, and the drop-down triangle appears, with the following effect:


To implement a drop-down menu, you first need to create a menu, action, and the button itself, in order to make it easier to display the effect, and add two member functions to display different information when the action is triggered:

M_menu = new Qmenu (this), m_addaction = new Qaction (m_menu); m_delaction = new Qaction (m_menu); M_addaction->settext ( Qobject::tr ("add")); M_delaction->settext (qobject::tr ("delete")); M_menu->addaction (m_addaction);m_menu-> Addaction (m_delaction), connect (m_addaction, &qaction::triggered, this, &buttonmenu::addfunc), connect (m_ Delaction, &qaction::triggered, this, &buttonmenu::d elfunc); M_pushbutton = new Qpushbutton (this); m_ Pushbutton->settext (QOBJECT::TR ("Test button menu"); M_pushbutton->setmenu (m_menu);//Settings menu
void Buttonmenu::addfunc () {qdebug () << "Addfunc";} void ButtonMenu::d elfunc () {qdebug () << "Delfunc";}
Compile run, there is a problem, Chinese characters garbled.


Because I am VS2013+QT, so solve the garbled problem in the header file add a sentence.

#pragma execution_character_set ("Utf-8")
For the drop-down triangle icon on the button, you can cancel it by QSS if you don't need it:

M_pushbutton->setstylesheet ("Qpushbutton::menu-indicator{image:none;}"); /Do not display the drop-down picture

You can see that the small triangle does not show:


If you need an action click, there is a clear indication, that is, the state, as follows:


You only need to set qaction to check on it:

M_addaction->setcheckable (TRUE);//Can checkm_delaction->setcheckable (true);//can check
Finally, run successfully, click the corresponding action, you can see the console output:



AC q:1245178753
This address: http://blog.csdn.net/u011417605/article/details/51218493

SOURCE Download: http://download.csdn.net/detail/u011417605/9499025

Qpushbutton drop-down menu (whether to cancel the drop-down triangle, check)

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.