c++ - 在派生自 QWidget 的主类中添加菜单

标签 c++ qt menu qwidget

我正在尝试在 Qt 中创建一个包含菜单的程序。主类派生自 QWidget,我知道我可以使用 QMainWindow 来使用函数 menuBar(),但我不能在 QMainWindow 中使用布局。我尝试使用 setMenuBar 在窗口的布局中添加 QMenuBar,但它不像使用 menuBar() 那样显示,而且我不知道如何使它成为下拉菜单。

This is the menu added with setMenuBar

This is the menu added with menuBar()

最佳答案

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);

在上面的代码中,我使用了小部件布局的菜单栏。

关于c++ - 在派生自 QWidget 的主类中添加菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684776/

相关文章:

c++ - 整数到 double 的转换

c++ - std::string.c_str() 使用什么编码?

c++ - 如何为 KDB+ 创建带有字符数组的批量对象

C++将两个QSliders加在一起?

qt - Qt qDebug()在Windows Shell中不起作用

android - 如何在 Android 应用程序中添加额外的菜单项?

java - 如何在Java/Android中制作单独的按钮功能?

c++ - "Smart"链接滚动条和编辑控件?

c++ - QChartView QLineSeries 通过鼠标点击选择

Android 选项菜单不显示