3

我很难以编程方式将菜单栏、菜单和子菜单添加到 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

4 回答 4

2

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

QMainWindow *w = new QMainWindow;

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

centralWidget->setLayout(L);
w->show();
于 2016-12-28T18:08:10.070 回答
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();
}

在此处输入图像描述

于 2016-12-28T18:17:09.247 回答
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

于 2019-06-06T08:14:23.713 回答
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();


}
于 2016-12-28T18:38:44.213 回答