C++ 关闭时的 Qt QMainWindow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3016899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:47:23  来源:igfitidea点击:

Qt QMainWindow at Close

c++qt

提问by Cenoc

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

这似乎是一个非常简单的问题,但我想在QMainWindow关闭时转储一些数据,所以我使用了以下代码:

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?.

但这似乎并没有让它调用close()。我这样做错了吗?
中央小部件不是应该被销毁吗?

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

或者应用程序在close()可以调用之前关闭?。

Any other ways of doing it then?

那还有什么别的办法吗?

采纳答案by Gy?rgy Andrasek

回答by mosg

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

你最MainWindow好像这样在你的主类中重新实现一个虚函数:

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.

祝你好运。

回答by Bill

Could you implement the closeEventfunction for your QMainWindowand put your code there?

你能实现你的closeEvent功能QMainWindow并将你的代码放在那里吗?

回答by Adam W

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

您最初的问题和代码不匹配。如果您想在QMainWindow创建子类并重新实现closeEvent或连接到MainWindow::destroyed(). 但是,请参阅第 3 段以获取注释。

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

但是您的代码显示了似乎是第三个类,该类将MainWindow被销毁的子代连接到某个名为close(). centralwidget将在 MainWindow 已经被销毁后被销毁,所以这很可能不会帮助你。

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).

此外,这取决于您如何创建 MainWindow(堆栈或堆)以及您是否正确销毁它。理想情况下,您应该创建一个子类QMainWindow(如果您使用了您可能已经拥有的设计器)。

回答by Paul-Sebastian Manole

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 closeEventdirectly to do what you need to do, like save state, sync data or whatever.

QMainWindow::closeEvent(QCloseEvent *)在你的课堂上实施。然后实现一个名为的新信号closing()并从您的QMainWindow::closeEvent(). 然后,您可以连接到该信号以在窗口关闭之前执行某些操作。您也可以closeEvent直接使用它来做您需要做的事情,例如保存状态、同步数据或其他任何事情。

回答by K.Mulier

enter image description hereIn Python (pyqt4 or pyqt5) you would need to do the following:

在此处输入图片说明在 Python(pyqt4 或 pyqt5)中,您需要执行以下操作:

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:

有趣的是closeEvent(..)函数中的内容在  窗口关闭之前被执行。您可以通过以下测试来验证这一点:

    # 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.

这证明在进入closeEvent(..)函数时窗口仍然可见,但在退出该函数后不可见。

回答by Rob

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

将转储代码添加到主窗口的析构函数如何?