实现右键菜单栏需要用到事件contextMenuEvent
在这个事件里面实例QMenu和QAction即可。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//主菜单栏设置
pop_menu = new QMenu(this);
new_action = new QAction("&new...",this);
open_action = new QAction(this);
close_action = new QAction(this);
open_action->setText("open");
close_action->setText("close");
//增加子菜单栏
QAction *child1 = new QAction("child1",this);
QAction *child2 = new QAction("child2",this);
QAction *child3 = new QAction("child3",this);
child = new QMenu("child",this);
child->addAction(child1);
child->addAction(child2);
child->addAction(child3);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::contextMenuEvent(QContextMenuEvent* e)