当我们需要在QT程序中添加右键弹出菜单时,可以使用QMenu和QAction类来实现。首先创建一个QMenu对象,然后添加QAction对象到菜单中。接着在需要右键弹出菜单的控件中调用QMenu的exec()函数即可显示菜单。具体操作步骤请看下面的例子代码:
QMenu menu = new QMenu(this);
QAction action1 = new QAction(tr("Action1"), this);
QAction *action2 = new QAction(tr("Action2"), this);
menu->addAction(action1);
menu->addAction(action2);
connect(action1, SIGNAL(triggered()), this, SLOT(onAction1Clicked()));
connect(action2, SIGNAL(triggered()), this, SLOT(onAction2Clicked()));
void onRightButtonClicked()
{
menu->exec(QCursor::pos());
}
暂无评论