Qt向QMainWindow添加menuBar、菜单和子菜单

Qt add menuBar, menus, and sub menus to QMainWindow

我很难以编程方式将菜单栏、菜单和子菜单添加到 Qt QMainWindow。

以下代码产生错误:

QWidget::setLayout: 试图在 QMainWindow "" 上设置 QLayout "",它已经有一个布局

注意事项: *.主window出来没有任何菜单或布局(空!)

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QMenuBar *menu = new QMenuBar;
            QMenu *file = new QMenu();
            file->addMenu("&File");
            menu->addMenu(file);

            QSlider *s1 = new QSlider(Qt::Horizontal);
               QSlider *s2 = new QSlider(Qt::Vertical);
               QSpinBox *sb = new QSpinBox;


               QHBoxLayout *L = new QHBoxLayout;
                L->addWidget(s1);
                L->addWidget(s2);
                L->addWidget(sb);




     QMainWindow *w = new QMainWindow;
     w->setLayout(L);
     w->show();

    return a.exec();
}

每个 QMainWindow 都应该有一个中央小部件:

QMainWindow *w = new QMainWindow;

QWidget* centralWidget = new QWidget;
w->setCentralWidget( centralWidget );

centralWidget->setLayout(L);
w->show();

向中央小部件添加布局:

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include<QMenuBar>
#include<QStatusBar>
#include <QMainWindow>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QMainWindow *w = new QMainWindow;

    QMenuBar *menu = new QMenuBar;
    QMenu *file = new QMenu();
    file->addMenu("&File");
    menu->addMenu(file);

    QWidget *centralwidget = new QWidget(w);
    w->setCentralWidget(centralwidget);

    QSlider *s1 = new QSlider(Qt::Horizontal, centralwidget);
    QSlider *s2 = new QSlider(Qt::Vertical, centralwidget);
    QSpinBox *sb = new QSpinBox;


    QHBoxLayout *L = new QHBoxLayout(centralwidget);
    L->addWidget(s1);
    L->addWidget(s2);
    L->addWidget(sb);
    w->show();

    return a.exec();
}

这是最终版本

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);


            QSlider *s1 = new QSlider(Qt::Horizontal);
               QSlider *s2 = new QSlider(Qt::Vertical);
               QSpinBox *sb = new QSpinBox;


     QMainWindow *w = new QMainWindow;

     QWidget *cw = new QWidget(w);

     QMenuBar *menu = new QMenuBar(cw);


     QHBoxLayout *L = new QHBoxLayout(cw);
      L->addWidget(s1);
      L->addWidget(s2);
      L->addWidget(sb);

             QMenu *file = new QMenu("&File");
             file->addMenu("Open");
              file->addMenu("new");

              QMenu *Build = new QMenu("&Build");
              Build->addAction("Rebuild this file");
               Build->addAction("Rebuild All");

             menu->addMenu(file);
             menu->addMenu(Build);

    w->setCentralWidget(cw);

     w->show();


     QObject::connect  (s1,SIGNAL(valueChanged(int) ),  sb,SLOT(setValue(int) )   );
     QObject::connect  (s1,SIGNAL(valueChanged(int) ),  s2,SLOT(setValue(int) )   );

     QObject::connect  (s2,SIGNAL(valueChanged(int) ),  sb,SLOT(setValue(int) )   );
     QObject::connect  (s2,SIGNAL(valueChanged(int) ),  s1,SLOT(setValue(int) )   );

     QObject::connect  (sb,SIGNAL(valueChanged(int) ),  s1,SLOT(setValue(int) )    );
     QObject::connect  (sb,SIGNAL(valueChanged(int) ),  s2,SLOT(setValue(int) )    );

     return a.exec();


}

为了清楚地回答标题提出的问题(这本身不是问题),QMainWindow 除了中央小部件等其他内容外,默认情况下已经有一个空的菜单栏。

要访问此 QMenuBar 并用您选择的内容填充它,只需从您的 QMainWindow 实例调用 menuBar()。 要向 QMenuBar 添加子菜单,请使用 QMenuBar::addMenu.

例如:

QAction* newAct = new QAction("save");
auto fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
auto submenu = fileMenu->addMenu("Submenu");
submenu->addAction(new QAction("action1");
submenu->addAction(new QAction("action2");

有关详细信息,请查看此 Qt 示例:https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html and also the QMenuBar reference https://doc.qt.io/qt-5/qmenubar.html#details