PDA

View Full Version : main window close not working



hvranic
17th June 2010, 18:21
new on forum, be gentle, thx for help.
qt version 4.6.2

This is code:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{

closeButton = new QPushButton("close");
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
setCentralWidget(closeButton);

bool x = false;
// here is problem
if ( !x )
close();
}

Q: why doesn't MainWindow closes?

Oximoron
17th June 2010, 18:30
Very simple.
In the doc it says:

Closes this widget. Returns true if the widget was closed; otherwise returns false.

First it sends the widget a QCloseEvent.

And a close event will be accepted by a visible widget.
A widget gets to be visible after it got a QShowEvent, and since you are still in the constructor, no such event has been received, hence the widget is not yet visible, hence, no effect of close().

Hardstyle
17th June 2010, 21:12
#include<qapplication.h>

connect(closeButton, SIGNAL(clicked()), qApp, SLOT(quit()));


this works