Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to hide Qmainwindow?

How to hide Qmainwindow?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 2.0k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    Hi,

    How can hide the Qmainwindow? when user click on close
    so, when i again open the window it will show with data

    Pl45m4P 1 Reply Last reply
    0
    • N n-2204

      Hi,

      How can hide the Qmainwindow? when user click on close
      so, when i again open the window it will show with data

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @n-2204

      Overwrite closeEvent and hide the window instead of closing it?
      But your approach seems weird. Overthink what you really want to do.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      N 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @n-2204

        Overwrite closeEvent and hide the window instead of closing it?
        But your approach seems weird. Overthink what you really want to do.

        N Offline
        N Offline
        n-2204
        wrote on last edited by
        #3

        @Pl45m4 Overwrite closeEvent and hide the window instead of closing it?
        yes, this way m looking actually but not working

        CP71C 1 Reply Last reply
        0
        • N Offline
          N Offline
          n-2204
          wrote on last edited by
          #4

          @Pl45m4

          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);
          
          S 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Can you explain exactly what your application is doing with its main window hidden ?
            How are you going to bring it back ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Can you explain exactly what your application is doing with its main window hidden ?
              How are you going to bring it back ?

              N Offline
              N Offline
              n-2204
              wrote on last edited by
              #6

              @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.

              Pl45m4P 1 Reply Last reply
              0
              • N n-2204

                @Pl45m4

                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);
                
                S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                @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: overwrite QMainWindow::closeEvent(...). This is how you normally do it. If you don't already have your own main window class derived from QMainWindow you should do that first. This is the normal approach to Qt.

                eventFilter() is really advanced and rarely needed.

                JonBJ 1 Reply Last reply
                1
                • S SimonSchroeder

                  @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: overwrite QMainWindow::closeEvent(...). This is how you normally do it. If you don't already have your own main window class derived from QMainWindow you should do that first. This is the normal approach to Qt.

                  eventFilter() is really advanced and rarely needed.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @SimonSchroeder
                  Hi Simon. For the record. I agree that the best is to sub-class and override closeEvent. However, I have seen examples suggested in this forum that if you do not want to/cannot sub-class, for whatever reason, you can use eventFilter to handle events without needing to subclass. Do you have any comment, is it not appropriate here? Just for my information.

                  S 1 Reply Last reply
                  0
                  • N n-2204

                    @Pl45m4 Overwrite closeEvent and hide the window instead of closing it?
                    yes, this way m looking actually but not working

                    CP71C Offline
                    CP71C Offline
                    CP71
                    wrote on last edited by
                    #9

                    @n-2204
                    This work!
                    Only then I must close it via task bar!
                    void MainWindow::closeEvent(QCloseEvent *event)
                    {
                    event->ignore();
                    hide();
                    }

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @SimonSchroeder
                      Hi Simon. For the record. I agree that the best is to sub-class and override closeEvent. However, I have seen examples suggested in this forum that if you do not want to/cannot sub-class, for whatever reason, you can use eventFilter to handle events without needing to subclass. Do you have any comment, is it not appropriate here? Just for my information.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @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.

                      1 Reply Last reply
                      2
                      • N n-2204

                        @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.

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by Pl45m4
                        #11

                        @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?

                        • https://doc.qt.io/qt-5/qtwidgets-desktop-systray-example.html

                        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();
                            }
                        }
                        

                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        2

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved