Qt:如何处理用户按下“X”(关闭)按钮的事件? [英] Qt: How do I handle the event of the user pressing the 'X' (close) button?
本文介绍了Qt:如何处理用户按下“X”(关闭)按钮的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用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?\n"),
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?\n"),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
}
if (resBtn == QMessageBox::Yes) {
QDialog::reject();
}
}
这篇关于Qt:如何处理用户按下“X”(关闭)按钮的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文