QMenu----QT鼠标右键弹出菜单

时间:2022-06-25 15:31:41

在用QT编程时,有时候要实现鼠标右键弹出菜单的功能.下面代码演示鼠标右键弹出菜单.

1.新建一个QT工程,点击MainWindow.ui,鼠标右键Go to slot.....,添加customContextMenuRequested(const QPoint &pos)事件

2.找到属性页中的contextMenuPolicy,把DefaultContextMenu改成CustomContextMenu.保存关闭MainWindow.ui

3.在on_MainWindow_customContextMenuRequested(const QPoint &pos)函数体添加一下代码

void MainWindow::on_MainWindow_customContextMenuRequested(const QPoint &/*pos*/)
{
    //创建菜单对象
    QMenu *pMenu = new QMenu(this);

    QAction *pNewTask = new QAction(tr("新建任务"), this);
    QAction *pEditTask = new QAction(tr("设置任务"), this);
    QAction *pDeleteTask = new QAction(tr("删除任务"), this);

    QAction *pToolRenName = new QAction(tr("改名工具"), this);
    QAction *pToolEdot = new QAction(tr("设置工具"), this);
    QAction *pToolDelete = new QAction(tr("删除工具"), this);

    //1:新建任务 2:设置任务 3:删除任务 4:改名工具 5:设置工具 6:删除工具
    pNewTask->setData(1);
    pEditTask->setData(2);
    pDeleteTask ->setData(3);
    pToolRenName->setData(4);
    pToolEdot->setData(5);
    pToolDelete ->setData(6);

    //把QAction对象添加到菜单上
    pMenu->addAction(pNewTask);
    pMenu->addAction(pEditTask);
    pMenu->addAction(pDeleteTask);
    pMenu->addAction(pToolRenName);
    pMenu->addAction(pToolEdot);
    pMenu->addAction(pToolDelete);

    //连接鼠标右键点击信号
    connect(pNewTask, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent()));
    connect(pEditTask, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent()));
    connect(pDeleteTask, SIGNAL(triggered()), SLOT(onTaskBoxContextMenuEvent()));
    connect(pToolRenName, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent()));
    connect(pToolEdot, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent()));
    connect(pToolDelete, SIGNAL(triggered()), SLOT(onTaskBoxContextMenuEvent()));

    //在鼠标右键点击的地方显示菜单
    pMenu->exec(cursor().pos());

    //释放内存
    QList<QAction*> list = pMenu->actions();
    foreach (QAction* pAction, list) delete pAction;
    delete pMenu;
}

void MainWindow::onTaskBoxContextMenuEvent()
{
    QAction *pEven = qobject_cast<QAction *>(this->sender()); //this->sender()就是发信号者 QAction

    //获取发送信息类型 1:新建任务 2:设置任务 3:删除任务 4:改名工具 5:设置工具 6:删除工具
    int iType = pEven->data().toInt();

    switch (iType)
    {
    case 1:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    case 2:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    case 3:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    case 4:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    case 5:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    case 6:
        QMessageBox::about(this, "tip", pEven->text());
        break;
    default:
        break;
    }
}

4.运行结果如下所示:

QMenu----QT鼠标右键弹出菜单