QT 鼠标右键菜单

重写函数 

void contextMenuEvent(QContextMenuEvent *)
void contextMenuEvent(QContextMenuEvent *)
{
    QMenu menu;
    QAction *addAction = menu.addAction(tr("Add Device"));
    QAction *settingAction = menu.addAction("Setting");

    QMenu menu1("test");
    QAction * test1 = menu1.addAction("test1");
    menu1.addSeparator();
    menu1.addAction("test2");

    menu.addMenu(&menu1);

    QAction *selectedAction = menu.exec(QCursor::pos());
    if(selectedAction == addAction) {
      qDebug() << "addAction";

      QDialog dialog ;
      dialog.exec();

    }
    else if (selectedAction == settingAction) {
      qDebug() << "settingAction";
    }
    else if (selectedAction == test1) {
      qDebug() << "test1";
    }


}

 


posted @ 2020-10-20 16:19  cicero  阅读(522)  评论(0编辑  收藏  举报