本文介绍了在从QWidget派生的主类中添加菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Qt中创建一个包含菜单的程序.主类是从QWidget派生的,我知道我可以使用QMainWindow来使用menuBar()函数,但不能在QMainWindow中使用布局.我试图使用setMenuBar在窗口的布局上添加QMenuBar,但它的显示效果不如使用menuBar(),而且我不知道如何使其成为下拉菜单.
I'm trying to create a program in Qt that contains menus. The main class is derived from QWidget, and I know I could use QMainWindow to use the function menuBar(), but I can't use layouts in QMainWindow. I tried to add a QMenuBar at the window's layout using setMenuBar, but it does not display like using menuBar() and I don't know how to make it a drop-down menu.
推荐答案
QVBoxLayout *boxLayout = new QVBoxLayout(this); // Main layout of widget
QMenuBar* menuBar = new QMenuBar();
QMenu *fileMenu = new QMenu("File");
menuBar->addMenu(fileMenu);
fileMenu->addAction("Save");
fileMenu->addAction("Exit");
this->layout()->setMenuBar(menuBar);
在上面的代码中,我使用了小部件布局的菜单栏.
In above code i had used menu bar of widget layout.
这篇关于在从QWidget派生的主类中添加菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!