为了在关闭继承自 QMainWindow 的窗口之前发出警告,我重新实现了它的closeEvent,当我close()手动发出时它可以正常工作。但是,单击“x”按钮不会触发此操作;它只是退出。
它确实aboutToQuit()为应用程序发出信号,我可以在窗口关闭后使用它来“恢复”窗口。但我想在最初关闭之前发出警告。
我不确定问题出在哪里。该窗口是顶级的,没有正在运行的线程。我是否误解了什么信号实际上连接到按钮点击......?它是 close()吧?
小智 5
在您的主窗口类标题中( closeEvent 必须是 virtual ):
public:
/*!
* \brief closeEvent
* \param event
*/
virtual void closeEvent ( QCloseEvent * event );
Run Code Online (Sandbox Code Playgroud)
然后在cpp
void MainWindow::closeEvent( QCloseEvent *event )
{
//! Ignore the event by default.. otherwise the window will be closed always.
event->ignore();
if(!EntitiesSaverObserver::Instance()->isAllSaved())
{
QMessageBox msgBox;
msgBox.setWindowIcon(QIcon(":/Resources/Icons/warning.png"));
msgBox.setIconPixmap(QPixmap(":/Resources/Icons/warning.png"));
QString strToShow = QString("Some Entities has been created or modified...");
msgBox.setText(strToShow);
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
{
// Save was clicked
qDebug() << "SAVE";
//! Do your stuff here
// ....
event->accept();
break;
}
case QMessageBox::Discard:
{
// Don't Save was clicked
qDebug() << "DISCARD";
event->accept();
break;
}
case QMessageBox::Cancel:
{
// Cancel was clicked
qDebug() << "CANCEL";
break;
}
default:
// should never be reached
break;
}
} else {
event->accept(); // Do not need to save nothing... accept the event and close the app
}
}
Run Code Online (Sandbox Code Playgroud)
此外,如果您想在工具栏中放置一个按钮作为 QAction,您可以连接信号,然后:
void MainWindow::on_actionExit_triggered()
{
close();
}
Run Code Online (Sandbox Code Playgroud)
这将调用主窗口的关闭事件。我希望这可以帮助你。
| 归档时间: |
|
| 查看次数: |
11792 次 |
| 最近记录: |