如何在退出时调用一个插槽
How to call a slot on quit
我想在Qt应用程序关闭之前更新我的数据库。
我想要像connect(this, SIGNAL(quit()), this, SLOT(updateDatabase()))
这样的东西一种方法是引入退出按钮,但是如果用户按下Alt+F4
,是否可以实现此功能呢?
用aboutToQuit()
信号代替
这个信号在应用程序即将退出主进程时发出事件循环,例如当事件循环级别降为零时。这可能发生在从应用程序内部调用quit()之后或当用户关闭整个桌面会话时。
如果应用程序必须执行某些操作,则该信号特别有用最后的清理工作。注意,这里不允许用户交互状态。
例如:
connect(this, SIGNAL(aboutToQuit()), this, SLOT(updateDatabase()));
还有另一种方法,不是aboutToQuit()
信号,而是重新实现 closeEvent(QCloseEvent *event)
。可以在语句event->accept()
;
:
void MainWindow::closeEvent(QCloseEvent *event)
{
call_your_slot_here();
// accept close event
event->accept();
}
相关文章:
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 一个类中的QT信号/插槽,但从不同的线程发出
- 为什么即使在使用 Qt::D irectConnection 之后,接收器的线程中仍会调用插槽?如何确保在另一个线程中调用它?
- 连接另一个在Qt中不起作用的类的插槽
- 是否可以将一个类别的插槽或常规功能从一个类连接到另一个类的插槽或常规功能?(QT)
- qnetworkAccessManager一个实例并连接插槽
- 将一个信号连接到多个插槽 Qt
- Qt移动插槽功能到另一个cpp文件
- 将一个对象的所有QT信号动态映射到一个插槽
- Qt - 解决一个插槽上的两个顺序调用,并且仅执行一次操作
- Qt:从一个对象发出相同的信号,但参数不同;插槽通过接收的参数进行区分
- Qt-有没有一种方法可以获得一个类中所有自定义插槽和信号的列表
- 如何使用信号和插槽从qt表单中获取id,然后在链表中搜索它并使用另一个表单显示结果
- Qt将GUI类中的方法连接到另一个类中的插槽
- 使用Qt信号和插槽更改另一个类的标签文本
- Qt:使用信号和插槽从另一个类调用MainWindow::Ui
- QSignalManager-无法将多个信号连接到一个插槽
- 如何在退出时调用一个插槽
- 如何使用 QWaitCondition 实现永久运行的 QThread,但在执行此操作时仍然需要捕获另一个插槽
- Qt/ c++如何在信号发出时等待一个插槽