我试图在QDialog中添加菜单栏但出现以下错误:
error: 'class Qt_Stackwidget' has no member named 'menuBar'
QMenuBar* menubar = this->menuBar();
我很困惑为什么会这样。我搜索并得到QDialog没有名为menuBar()的函数函数。如何将菜单栏添加到QDialog。有谁可以帮我解决这个问题?
答案 0 :(得分:2)
您可以添加这样的菜单栏:
#include <QtGui>
class Dialog : public QDialog
{
public:
Dialog(QWidget *parent = 0) : QDialog(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
QMenu *menu = new QMenu("File");
menu->addAction("Exit");
QMenuBar *menubar = new QMenuBar();
menubar->addMenu(menu);
layout->setMenuBar(menubar);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *button = new QPushButton("Button");
Dialog *dialog = new Dialog;
dialog->layout()->addWidget(button);
dialog->show();
return app.exec();
}