简体   繁体   English

setCentralWidget为什么不起作用?

[英]Why doesn't setCentralWidget work?

Here is my class MainWindow : 这是我的类MainWindow:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    menu* v = new menu(this);
    setCentralWidget(v);
}

And my menu class : 和我的菜单类:

menu::menu(MainWindow* parent){
    QLabel l = new QLabel("123");
    QVBoxLayout* lay = new QVBoxLayout;
    lay->addWidget(l);
    this->setLayout(lay);
    QWidget* a = new QWidget;
    QVBoxLayout* lay2 = new QVBoxLayout;
    QLabel* ll = new QLabel("456");
    lay2->addWidget(ll);
    a->setLayout(lay2);
    parent->setCentralWidget(a);
}

When I run the program, the window shows 123 but I would like it to show 456. 当我运行程序时,窗口显示123,但我希望显示456。

Is the method setCentralWidget not working? setCentralWidget方法setCentralWidget吗?

setCentralWidget works ok. setCentralWidget工作正常。 You are mixing up your widget menu construction with its position in an external widget ( MainWindow ). 您正在将窗口小部件menu结构及其在外部窗口小部件( MainWindow )中的位置混合在一起。 You should keep these thing well separated, or you won't be able, for example, to use menu inside other widgets. 您应该将这些东西很好地分隔开,否则,例如,您将无法使用其他小部件内的menu

So, you should set the appearance of menu in the constructor, and call setCentralWidget only in MainWindow . 因此,您应该在构造函数中设置menu的外观,并仅在MainWindow调用setCentralWidget

It should look like: 它应该看起来像:

file.h 文件

menu::menu(QWidget* parent = 0);

file.cpp file.cpp

menu::menu(QWidget* parent) : QWidget(parent)
{   
    // Create items
    QLabel* l = new QLabel("123", this);
    QLabel* ll = new QLabel("456", this);

    // Put items in layout
    QVBoxLayout* lay = new QVBoxLayout();
    lay->addWidget(l);
    lay->addWidget(ll);

    // Set "lay" as the layout of this widget
    setLayout(lay);
}

UPDATE 更新

Since the wanted behavior is to have an interface that switch view according to button clicks: 由于所需的行为是具有一个界面,该界面可以根据按钮的点击切换视图:

在此处输入图片说明

the best option is to use a QStackedWidget . 最好的选择是使用QStackedWidget

Here a sample code the will produce this interface using QStackedWidget . 这里是一个示例代码,它将使用QStackedWidget生成此接口。

widget1.h widget1.h

#ifndef WIDGET1
#define WIDGET1

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

class Widget1 : public QWidget
{
    Q_OBJECT
public:
    Widget1(QWidget* parent = 0) : QWidget(parent) {
        QPushButton* btn = new QPushButton("Button Widget 1", this);
        QVBoxLayout* layout = new QVBoxLayout();
        layout->addWidget(btn);
        setLayout(layout);
        connect(btn, SIGNAL(clicked()), SIGNAL(buttonClicked()));
    }

signals:
    void buttonClicked();
};

#endif // WIDGET1

widget2.h widget2.h

#ifndef WIDGET2
#define WIDGET2

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

class Widget2 : public QWidget
{
    Q_OBJECT
public:
    Widget2(QWidget* parent = 0) : QWidget(parent) {
        QPushButton* btn1 = new QPushButton("Button 1 Widget 2", this);
        QPushButton* btn2 = new QPushButton("Button 2 Widget 2", this);
        QVBoxLayout* layout = new QVBoxLayout();
        layout->addWidget(btn1);
        layout->addWidget(btn2);
        setLayout(layout);
        connect(btn2, SIGNAL(clicked()), SIGNAL(button2Clicked()));
    }

signals:
    void button1Clicked();
    void button2Clicked();
};

#endif // WIDGET2

mainwindow.h 主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void buttonWidget1Clicked();
    void button2Widget2Clicked();
private:
    QStackedWidget* m_sw;
};

#endif // MAINWINDOW_H

mainwindow.cpp 主窗口

#include "mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include "widget1.h"
#include "widget2.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create Widgets
    Widget1* w1 = new Widget1(this);
    Widget2* w2 = new Widget2(this);
    QLabel* w3 = new QLabel("Result", this);

    m_sw = new QStackedWidget(this);
    m_sw->addWidget(w1);
    m_sw->addWidget(w2);
    m_sw->addWidget(w3);

    setCentralWidget(m_sw);

    connect(w1, SIGNAL(buttonClicked()), this, SLOT(buttonWidget1Clicked()));
    connect(w2, SIGNAL(button2Clicked()), this, SLOT(button2Widget2Clicked()));
}

void MainWindow::buttonWidget1Clicked()
{
    m_sw->setCurrentIndex(1); // Will show Widget2
}

void MainWindow::button2Widget2Clicked()
{
    m_sw->setCurrentIndex(2); // Will show Widgee3
}

MainWindow::~MainWindow() {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM