Qt将菜单栏,菜单和子菜单添加到QMainWindow

时间:2016-12-28 17:36:47

标签: c++ qt qt5 qmainwindow qmenubar

给所有人光荣,

我很难使用代码向Qt QMainWindow添加菜单栏,菜单和子菜单。

以下代码正在运行并产生错误:

QWidget :: setLayout:尝试在QMainWindow上设置QLayout“”,已经有布局

备注: *。主窗口没有任何菜单或布局(空!)

#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();
}

4 个答案:

答案 0 :(得分:1)

将布局添加到中央窗口小部件:

#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();
}

enter image description here

答案 1 :(得分:0)

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

QMainWindow *w = new QMainWindow;

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

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

答案 2 :(得分:0)

这是最终版本

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


}

答案 3 :(得分:0)

为试图清楚地回答标题所建议的问题(本身不是问题),默认情况下,QMainWindow确实已经有一个空的菜单栏,除了诸如中央小部件之类的其他东西之外。

要访问此QMenuBar并用您选择的内容填充它,只需从您的menuBar()实例调用QMainWindow。 要将子菜单添加到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以及QMenuBar参考https://doc.qt.io/qt-5/qmenubar.html#details