Solved How to use "try destroy window" event?
-
I have descendent by QMainWindow, QTabWidget with editors CodeEditor : public QPlainTextEdit.
Editor can be closed by many way : click X on tab, Ctrl+F4, menu File->Close and even exit program witgh Alt+F4.
I want not enable closing not saved editors until user not choose (Yes,No, Cancel).
How do it? -
Hi
You are in luck. That use case is the example from the docs :)
https://doc.qt.io/qt-5/qwidget.html#closeEventSo you override this function and its called when user tried to close app and then you can
reject it or allow it as you wish. -
But how to do with closeEvent not in MainWindow but CodeEditor which is tabWidget widget?
void CodeEditor::closeEvent(QCloseEvent *event) { if (document()->isModified()){ event->ignore(); } else { event->accept(); } }
when I debug, it call ignore when modified but anyway widget is closed
-
But is CodeEditor inside other widget ?
Else im not really sure why ignore wont work. -
Editor is inside tab
newEditor = new CodeEditor(repository, filePath, this); newEditor->openFile(filePath); ... tabWidget->addTab(newEditor, title); tabWidget->setCurrentWidget(newEditor);
-
@AndrzejB
Ok :)
You need to handle the closeEvent at the top level widget as the childs cant cancel the parent closing (AFAIK)
So who ever holds the tabWidget, would be a good canidate to control it. -
I have method
void TabWindow::closeTab(int index)
where "TabWindow" is my main window. I simply change this method. This method cover closing by tab,by menu and by shortcuts. Only closing whole app is not covered by this method, but closing app event you give me above, thanks!
-
@AndrzejB
Ah super. :)
Don't forget to mark as solved if you are satisfied.