How to hide Qmainwindow?
-
bool Falyn::eventFilter(QObject* l_obj, QEvent* l_event) { QEvent::Type l_eventType = l_event->type(); switch (l_eventType) { case QEvent::Close: exitInvoked(); l_event->ignore(); break; default: return QObject::eventFilter(l_obj, l_event); } return true; } void Falyn:: exitInvoked() { this->hide(); //this is not hiding // this->showMinimized(); if do minimzie then its working // this->close(); } in constructor installEventFilter(this);
-
Hi,
Can you explain exactly what your application is doing with its main window hidden ?
How are you going to bring it back ? -
@SGaist
I have added this Qmainwindow(Falyn) in a action (of my project circle) , when user click on Falyn the Qmainwindow will appear and when user clicks on close it should not close, I don't want to lose the data so I want to hide the Qmaindwindow, and when user again open(click on action of circle window) Falyn the window should show without loss of data. -
@n-2204 said in How to hide Qmainwindow?:
installEventFilter(this);
This does not make any sense. The event filter needs to be installed on the main window (as far as I understood Falyn is not the main window). However, if you want to react to QEvent::Close inside the same object that receives the event, you should overwrite
event(QEvent*)
instead. But, as suggested before, it is even easier: overwriteQMainWindow::closeEvent(...)
. This is how you normally do it. If you don't already have your own main window class derived fromQMainWindow
you should do that first. This is the normal approach to Qt.eventFilter()
is really advanced and rarely needed. -
@SimonSchroeder
Hi Simon. For the record. I agree that the best is to sub-class and overridecloseEvent
. However, I have seen examples suggested in this forum that if you do not want to/cannot sub-class, for whatever reason, you can useeventFilter
to handle events without needing to subclass. Do you have any comment, is it not appropriate here? Just for my information. -
@JonB said in How to hide Qmainwindow?:
Do you have any comment, is it not appropriate here? Just for my information.
I personally don't subclass every single widget I use (it is actually very, very few). In general there is nothing wrong with using eventFilter. Especially when using Qt Designer it is a lot easier to use an existing widget and just add an event filter (slightly simpler than promoting to your own widgets inside Qt Designer).
If I have existing code and want to add some behavior (by reacting to events) in just a single place, then I use eventFilter() because the change is much easier. (This goes against proper software engineering principles because a derived widget would be more reusable. I don't like to come up with many different class names. And I personally don't believe everything should be designed to be reusable right from the start. You might make something more complicated which in the end is never reused anyway.)
My experience (and also what Qt Creator's wizard suggests) is that 99% of the time you have your own MainWindow instead of just a plain QMainWindow. Especially when your application grows eventually it makes more sense to put stuff directly inside a class derived from QMainWindow. You should set up your UI inside the constructor of MainWindow. And if your application is supposed to do something MainWindow also will have some slots. IMHO you shouldn't put all this into
main()
. This makes QMainWindow the special case where you should definitely derive and override instead of using an event filter. For other specialized widgets I agree with you. -
@n-2204 said in How to hide Qmainwindow?:
when user click on Falyn the Qmainwindow will appear and when user clicks on close it should not close, I don't want to lose the data so I want to hide the Qmaindwindow, and when user again open(click on action of circle window) Falyn the window should show without loss of data.
How about minimizing the app to system tray?
Also, in the system tray example there is exactly what you could do:
void Window::closeEvent(QCloseEvent *event) { #ifdef Q_OS_MACOS if (!event->spontaneous() || !isVisible()) { return; } #endif if (trayIcon->isVisible()) { QMessageBox::information(this, tr("Systray"), tr("The program will keep running in the " "system tray. To terminate the program, " "choose <b>Quit</b> in the context menu " "of the system tray entry.")); hide(); event->ignore(); } }