我正在使用 Qt 开发应用程序.我不知道哪个插槽对应于用户单击窗口框架的'X'(关闭)按钮"的事件,即这个按钮:
I am developing an application using Qt. I don't know which slot corresponds to the event of "the user clicking the 'X'(close) button of the window frame" i.e. this button:
如果没有用于此的插槽,任何人都可以建议我一些其他方法,以便在用户按下关闭按钮后我可以启动功能.
If there isn't a slot for this, can anyone suggest me some other method by which I can start a function after the user presses that close button.
如果你有一个 QMainWindow
你可以覆盖 closeEvent
方法.
If you have a QMainWindow
you can override closeEvent
method.
#include <QCloseEvent>
void MainWindow::closeEvent (QCloseEvent *event)
{
QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME,
tr("Are you sure?
"),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resBtn != QMessageBox::Yes) {
event->ignore();
} else {
event->accept();
}
}
如果您要继承 QDialog
,则不会调用 closeEvent
,因此您必须覆盖 reject()
:
If you're subclassing a QDialog
, the closeEvent
will not be called and so you have to override reject()
:
void MyDialog::reject()
{
QMessageBox::StandardButton resBtn = QMessageBox::Yes;
if (changes) {
resBtn = QMessageBox::question( this, APP_NAME,
tr("Are you sure?
"),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
}
if (resBtn == QMessageBox::Yes) {
QDialog::reject();
}
}
这篇关于Qt:如何处理用户按下“X"(关闭)按钮的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!