Woody
Woody

Reputation: 107

Why doesn't setCentralWidget work?

Here is my class 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.

Is the method setCentralWidget not working?

Upvotes: 0

Views: 1743

Answers (1)

Miki
Miki

Reputation: 41765

setCentralWidget works ok. You are mixing up your widget menu construction with its position in an external widget (MainWindow). You should keep these thing well separated, or you won't be able, for example, to use menu inside other widgets.

So, you should set the appearance of menu in the constructor, and call setCentralWidget only in MainWindow.

It should look like:

file.h

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

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:

enter image description here

the best option is to use a QStackedWidget.

Here a sample code the will produce this interface using QStackedWidget.

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

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

Upvotes: 2

Related Questions