Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QMainWindow at Close

Tags:

c++

qt

this may seem like a very simple question, but I want to dump some data whenever the QMainWindow closes, so I used the following piece of code:

QObject::connect(MainWindow.centralwidget, SIGNAL(destroyed()), this, SLOT(close()));

But this doesn't seem to make it call close(). Am I doing this wrong?.
Isn't the centralwidget suppose to be destroyed?.

Or perhaps the application closes before close() can be called?.

Any other ways of doing it then?

like image 687
Cenoc Avatar asked Jun 10 '10 17:06

Cenoc


3 Answers

You better to re implement one virtual function in your main MainWindow class like this:

class MainWindow : public QMainWindow {

    Q_OBJECT;

public:
    MainWindow();

protected:
     void closeEvent(QCloseEvent *event);
}

and then declare in source file:

void MainWindow::closeEvent(QCloseEvent *event) {
     // do some data saves or something else
}

Good luck.

like image 183
mosg Avatar answered Oct 07 '22 07:10

mosg


I'd try QGuiApplication::lastWindowClosed() instead.

like image 32
György Andrasek Avatar answered Oct 07 '22 09:10

György Andrasek


Could you implement the closeEvent function for your QMainWindow and put your code there?

like image 33
Bill Avatar answered Oct 07 '22 07:10

Bill