[QT Development] add context menu events in QT Gui

Source: Internet
Author: User

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

The slot function customcontextmenurequested can be used.(QpointPos).

 

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

-->Select customcontextmenurequested(QpointPos).

Then, in the newly created customcontextmenurequested(QpointPos) Function to implement the specific menu bar.

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:

Void mainwindow: on_treewidget_customcontextmenurequested (qpoint POS) <br/>{< br/> qtreewidgetitem * curitem = UI-> treewidget-> itemat (POS ); // obtain the currently clicked node <br/> If (curitem = NULL) return; // In this case, the right-click position is not within the treeitem range, right-click <br/> If (0 = curitem-> data (0, QT: userrole) // data (...) the returned data has been set with setdata () before the node is created <br/>{< br/> qmenu * popmenu = new qmenu (this ); // define a shortcut menu </P> <p> popmenu-> addaction (ui-> action_newdb ); // Add a qaction to the menu. The action is defined by the designer <br/> popmenu-> addaction (ui-> action_opendb ); <br/> popmenu-> addaction (ui-> action_deldb); <br/> popmenu-> exec (qcursor: pos (); // right-click the menu, the menu position is the cursor position <br/>}< br/> else <br/>{< br/> qmenu * popmenu = new qmenu (this ); // define a shortcut menu </P> <p> popmenu-> addaction (ui-> action_newtable ); // Add a qaction to the menu. The action is defined by the designer <br/> popmenu-> addaction (ui-> action_opentable ); <br/> popmenu-> addaction (ui-> action_designtable); <br/> popmenu-> exec (qcursor: pos (); // The right-click menu is displayed, the menu position is the cursor position <br/>}< br/>

Code implementation for creating the treewidget root node:

Qtreewidgetitem * root; <br/> root = new qtreewidgetitem (ui-> treewidget, qstringlist (qstring ("connection"); <br/> qvariant var0 (0 ); <br/> root-> setdata (0, QT: userrole, var0 );

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.