1. Include the relevant header file in the. h file
#include <QMenu><QContextMenuEvent>
2. Define an Action object in the. h file
Qaction **del;
3. Declaring a private method in an. h file
void contextmenuevent (qcontextmenuevent *e);
4. Add right-click after action slot declaration in. h file
Private Slots: void edit_line (); void del_line ();
5. Instantiate an Action object in the build function of the CPP file
Edit =NewQaction (Qicon (":/icon/edit.png"), TR ("Edit"), ThisConnect (edit, SIGNAL (triggered)), This, SLOT (Edit_line ()));d El=NewQaction (Qicon (":/icon/delete.png"), TR ("Delete"), This), connect (Del, SIGNAL (triggered ()), This, SLOT (Del_line ()));
6. Implement right-click events in CPP files
void Mainwindow::contextmenuevent (qcontextmenuevent *e) { new qmenu ();
menu->setfixedwidth (+); Menu,addaction (edit); Menu, Addaction (del); Menu->exec (e->globalpos ()); Delete menu;}
Finally implement the post-click Processing in the CPP file
void Mainwindow::edit_line () { //put your code}void MainWindow:: Del_line () { //put your code}
QT5 Add right-click menu Simple Test