問題描述
我正在使用 Qt 開發(fā)應(yīng)用程序.我不知道哪個(gè)插槽對(duì)應(yīng)于用戶單擊窗口框架的'X'(關(guān)閉)按鈕"的事件,即這個(gè)按鈕:
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:
如果沒有用于此的插槽,任何人都可以建議我一些其他方法,以便在用戶按下關(guān)閉按鈕后我可以啟動(dòng)功能.
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.
推薦答案
如果你有一個(gè) 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
,則不會(huì)調(diào)用 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();
}
}
這篇關(guān)于Qt:如何處理用戶按下“X"(關(guān)閉)按鈕的事件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!