开发者

Qt QMainWindow at Close

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 befor开发者_StackOverflow社区e close() can be called?.

Any other ways of doing it then?


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.


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


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


Qt QMainWindow at Close

In Python (pyqt4 or pyqt5) you would need to do the following:

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        #
        # My initializations...
        #

    ''''''

    def closeEvent(self, *args, **kwargs):
        #
        # Stuff I want to do when this
        # just before (!) this window gets closed...
        #

    ''''''

It is interesting to know that the stuff in the closeEvent(..) function gets executed just before  the window closes. You can verify this with the following test:

    # Test if the closeEvent(..) function
    # executes just before or just after the window closes.
    def closeEvent(self, *args, **kwargs):
        # 'self' is the QMainWindow-object.
        print(self)
        print(self.isVisible())

        # Print out the same stuff 2 seconds from now.
        QTimer.singleShot(2000, lambda: print(self))
        QTimer.singleShot(2100, lambda: print(self.isVisible()))

    ''''''

This is the output in your terminal:

<myProj.MyWindow object at 0x000001D3C3B3AAF8>
True

<myProj.MyWindow object at 0x000001D3C3B3AAF8>
False

This proves that the window was still visible when entering the closeEvent(..) function, but not after exiting that function.


Your initial question and code don't match. If you want to do something on the QMainWindow either create a sub-class and re-implement closeEvent or connect to MainWindow::destroyed(). See the 3rd paragraph for a note however.

But your code is showing what appears to be a 3rd class that is connecting a child of the MainWindow being destroyed to some slot called close(). centralwidget will be destroyed AFTER MainWindow is already destroyed so this most likely won't help you anyway.

Also, this depends on how you created MainWindow (stack or heap) and if you are destructing it properly. Ideally, you should create a subclass of QMainWindow (which if you used the designer you probably already have).


Implement QMainWindow::closeEvent(QCloseEvent *) in your class. Then implement a new signal called closing() and emit it from your implementation of QMainWindow::closeEvent(). Then you can connect to that signal to do something right before the window is closed. You can also use closeEvent directly to do what you need to do, like save state, sync data or whatever.


How about adding the dump code to your main windows' destructor?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜