c++ 向QMainWindow添加菜单栏、菜单和子菜单

gg58donl  于 2023-01-03  发布在  其他
关注(0)|答案(5)|浏览(204)

我有一个困难的时间添加菜单栏,菜单和子菜单到Qt QMainWindow编程。
下面的代码会产生错误:

QWidget::设置布局:试图在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();
}
4nkexdtk

4nkexdtk1#

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

QMainWindow *w = new QMainWindow;

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

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

bvn4nwqk2#

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

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

nukf8bse

nukf8bse3#

这是最终版本

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

}
rqdpfwrv

rqdpfwrv4#

为了试图清楚地回答标题所暗示的问题(它本身不是一个问题),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");

For more information, look at this Qt example : 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

62o28rlo

62o28rlo5#

我知道这是一个古老的职位,但答案是不必要的复杂
基本Qt Widget应用程序

QApplication a(argc, argv);
MainWindow w;

w.menuBar()->addAction("TEST");

w.show();

addAction("TEST"); 

     has 4 overloads to actually implement the menu option

相关问题