QT5 Add right-click menu Simple Test

Source: Internet
Author: User

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

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.