本人在制作一个文本编辑器,想模仿gedit关闭时的功能,即在文本修改后,点窗口的关闭按钮,会弹出要求用户保存的信息。
具体如图:
我在QT里写了一个
connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(MyProgramClose()));
来捕获关闭信号,在MyProgramClose函数中,加入一个保存的对话框。如果选择取消时,应该是不关闭主程序窗口,但是我发现,保存对话框是出来了,主程序窗口也没有了。
MyProgramClose函数如下:
- C/C++ code
QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setStandardButtons(QMessageBox::SaveAll | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::SaveAll); int ret = msgBox.exec(); switch (ret) { case QMessageBox::SaveAll: break; case QMessageBox::Discard: break; case QMessageBox::Cancel: return; break; default: break; }
不知道是我获取关闭信号获取错了,还是我在Cancel里的操作不正确?希望各位高人们指点指点!!
------解决方案--------------------
建议override editor的closeEvent事件函数
------解决方案--------------------
connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(MyProgramClose()));
这样达不到你想要的效果;
help doc:
void QCoreApplication::aboutToQuit () [signal]
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state