QT 右键菜单

QMenu *pMenu = new QMenu(this);

    QAction *pTest1 = new QAction(QIcon("copy.ico"),"复制", this);

    QAction *pTest2 = new QAction("删除", this);

    QAction *pTest3 = new QAction("标记", this);

    QAction *pTest4 = new QAction("取消标记", this);

  //把QAction对象添加到菜单上

    pMenu->addAction(pTest1);

    pMenu->addAction(pTest2);

    pMenu->addAction(pTest3);

    pMenu->addAction(pTest4);

    //添加分隔线

    //pMenu->addSeparator();

    //设置点击后发送的数据

  // pTest1->setData(1);

    //连接鼠标右键点击信号

    connect(pTest1, &QAction::triggered, this, &sub::ClickCopy);

    connect(pTest2, &QAction::triggered, this, &sub::ClickDel);

    connect(pTest3, &QAction::triggered, this, &sub::Clicktink);

    connect(pTest4, &QAction::triggered, this, &sub::ClickDeltink);

    //在鼠标右键点击的地方显示菜单

    pMenu->exec(cursor().pos());

    //释放内存,若此处不手动释放,则必须等到程序结束时才都能释放

    QList list = pMenu->actions();

    foreach (QAction* pAction, list)

        delete pAction;

    delete pMenu;

你可能感兴趣的:(QT 右键菜单)