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 );