Qt right-click menu example

Source: Internet
Author: User

To add a right-click menu bar in a form, for example, add a menu bar in qtreewidget,

You can use the customcontextmenurequested (qpoint POS) function ).

 

If it is in QT creator, right-click qtreewidget --> go to slot --

--> Select customcontextmenurequested (qpoint POS ).

 

Then, you can implement the specific menu bar in the newly created customcontextmenurequested (qpoint POS) function.

 

In order to determine the right-click position, that is, the points are different item nodes, different right-click menus are displayed. We can use the itemat () function in the API.

To assign different key values to each node, you can use setdata.

 

I added the right-click menu implementation to treewidget.Code:

 

Code implementation for creating the treewidget root node:

 

Qtreewidgetitem * root; root = new qtreewidgetitem (ui-> treewidget, qstringlist (qstring ("connection"); qvariant var0 (0); root-> setdata (0, QT:: userrole, var0); void mainwindow: on_treewidget_customcontextmenurequested (qpoint POS) {qtreewidgetitem * curitem = UI-> treewidget-> itemat (POS ); // obtain the currently clicked node if (curitem = NULL) return; // In this case, the right-click position is not within the treeitem range, right-click if (0 = curitem-> data (0, QT: userrole)/data (...) the returned data has already been set {qmenu * popmenu = new qmenu (this) with setdata () when the node was created ); // define a right-click menu popmenu-> addaction (ui-> action_newdb ); // Add qaction to the menu. This action defines popmenu-> addaction (ui-> action_opendb); popmenu-> addaction (ui-> action_deldb) with the designer ); popmenu-> exec (qcursor: pos (); // right-click the menu and select the cursor position.} else {qmenu * popmenu = new qmenu (this ); // define a right-click menu popmenu-> addaction (ui-> action_newtable ); // Add qaction to the menu. This action defines popmenu> addaction (ui-> action_opentable) with the designer. popmenu-> addaction (ui-> action_designtable ); popmenu-> exec (qcursor: pos (); // right-click the menu and select the cursor position }}

Note:

Add the following to the mainwindow constructor:

UI-> treewidget-> setcontextmenupolicy (QT: customcontextmenu );

Otherwise, no right-click is displayed !!

 
QtBuilt-in controls to add the right-click menu method (QtreewidgetRight-click a menu as an example ):
 
Connect(Moduletree,Signal(Itempressed(Qtreewidgetitem *, Int)),
This,Slot(Slotitemclicked(Qtreewidgetitem *, Int)));
 
Implementation slot Functions
 
 void cguimainwindow: slotitemclicked (qtreewidgetitem * item, int column) {If (qapp-> mousebuttons () = QT: leftbutton) {return;} If (qapp-> mousebuttons () = QT: rightbutton) {If (item-> childcount ()> 0) {return;} showmenu ();}} 
   menu display function:  
 void cguimainwindow :: showmenu () {qpoint Pos; qmenu menu (moduletree); qicon myicon ("Resources/logo.png"); menu. addaction (myicon, TR ("menu _ 1"); menu. addaction (myicon, TR ("menu _ 2"); menu. addaction (myicon, TR ("menu _ 3"); menu. addaction (myicon, TR ("menu _ 4"); menu. addaction (myicon, TR ("menu _ 5"); menu. addaction (myicon, TR ("menu _ 6"); menu.exe C (qcursor: pos ());} 

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.