Display other QWidget Qt parent window, must handle to be displayed in the initi

A form, which wants to display a from other QWidget content, encountered a very strange question, please help me look for reason daniel. Thank you.

The code is very simple
The main interface of mainwindow.h
#include <QMainWindow>
#include "child.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
Child * child ;
};


mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
child =new Child(this);

}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
child->init();
}


Child.h interface
#include <QWidget>
#include <QLabel>

class Child : public QWidget
{
Q_OBJECT
public:
explicit Child(QWidget *parent = 0);
void init();

signals:

public slots:
private:
QLabel * label;

};

child.cpp

Child::Child(QWidget *parent) :
QWidget(parent)
{
}
void Child::init()
{
label = new QLabel(this);
label->setText("come in");
}



Strange question came:
If you write directly in the constructor of the MainWindow.cpp
child =new Child(this);
child->init();
It can display,

If you click on the button function on_pushButton_clicked in MainWindow.cpp (in writing):
child =new Child(this);
child->init();
child->show();You can display properly

But if the child =new Child (this); code in the constructor, it cannot display child, or without the show, not child... Think for a long time did not understand why.. Please advice.. Thank you

Started by Esther at December 14, 2016 - 12:49 PM

Hello, a QWidget forms can only install a layout, multiple layout, you must delete the previous layout

Posted by Miranda at December 23, 2016 - 1:04 PM