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