PDA

View Full Version : Create QMainWindow in runtime



qt_developer
25th June 2012, 18:53
Hi all,

I want to create a QMainWindow when I click in a QPushButton.

I have this code in the SLOT of the QPushButton that is called when the clicked() SIGNAL is emitted:



QMainWindow* myQMainWindow = new QMainWindow(this);
myQMainWindow->setWindowModality(Qt::WindowModal);
myQMainWindow->setGeometry(geometry());
myQMainWindow->move(QPoint(100, 100));

QVBoxLayout *layout = new QVBoxLayout(myQMainWindow->centralWidget());
myQMainWindow->setLayout(layout);
QPushButton* pushButton = new QPushButton("hello", myQMainWindow->centralWidget());
myQMainWindow->show();


The window appears but the button with text "hello" doesn't shown. Why?

Regards.

code_err
25th June 2012, 19:10
Maybe you have to add button to layout?

qt_developer
25th June 2012, 19:22
I have solved it.

I have created a QWidget and I have called to QMainWindow::setCentralWidget() method.

Regards.