Qt中实现窗口右键菜单

来源:互联网 发布:法兰克福大学知乎 编辑:程序博客网 时间:2024/06/08 04:14

1.在Qt设计师中选中mainwindow,将contextMenuPolicy属性修改为CustomContextMenu.

2.在菜单中选编辑信号与槽,点击主窗口空白处。会弹出如下


3.选择customContextMenuPequested(QPoint)  信号和  自定义的槽   本次中为on_mainwindow_right_clicked()

4,.编辑槽的代码

void MainWindow::on_mainwindow_right_Clicked(){   QMenu *popMenu =new QMenu(this);//定义一个右键弹出菜单    popMenu->addAction(ui->action_Cut);//往菜单内添加QAction   该action在前面用设计器定义了    popMenu->addAction(ui->action_Copy);    popMenu->addAction(ui->action_Paste);    popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置}

5.over

0 0
原创粉丝点击