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. Close non-modal window when a model window is opened?
Forum Updated to NodeBB v4.3 + New Features

Close non-modal window when a model window is opened?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 897 Views 2 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    Hi, the below code executes but does not close my widget window:

    void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
    {
        if(this->isVisible() && focusWindow->isModal())
            this->close();
    }
    

    MyWidget is non-modal window, the above slot is connected to the void QGuiApplication::focusObjectChanged(QObject *focusObject) signal. The slot executes when I open a modal window and focusWindow->isModal() returns true, but the instance of MyWidget is not closed, why?

    jsulmJ 1 Reply Last reply
    0
    • CJhaC CJha

      Hi, the below code executes but does not close my widget window:

      void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
      {
          if(this->isVisible() && focusWindow->isModal())
              this->close();
      }
      

      MyWidget is non-modal window, the above slot is connected to the void QGuiApplication::focusObjectChanged(QObject *focusObject) signal. The slot executes when I open a modal window and focusWindow->isModal() returns true, but the instance of MyWidget is not closed, why?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @CJha said in Close non-modal window when a model window is opened?:

      why?

      Does your app enter the if block (so, is this->close() executed)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      CJhaC 1 Reply Last reply
      0
      • jsulmJ jsulm

        @CJha said in Close non-modal window when a model window is opened?:

        why?

        Does your app enter the if block (so, is this->close() executed)?

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @jsulm Yes, it enters the block and executes any code I put there, I tried:

        void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
        {
            if(this->isVisible() && focusWindow->isModal()){
                qDebug() << "Will Close";
                this->close();
            }
        }
        
        void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
        {
            if(this->isVisible() && focusWindow->isModal())
                this->hide();
        }
        

        Both qDebug() << "Will Close"; and this->hide(); works as expected.

        JonBJ 1 Reply Last reply
        0
        • CJhaC CJha

          @jsulm Yes, it enters the block and executes any code I put there, I tried:

          void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
          {
              if(this->isVisible() && focusWindow->isModal()){
                  qDebug() << "Will Close";
                  this->close();
              }
          }
          
          void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow)
          {
              if(this->isVisible() && focusWindow->isModal())
                  this->hide();
          }
          

          Both qDebug() << "Will Close"; and this->hide(); works as expected.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @CJha
          This is perhaps a possibility, I'm not sure how modal blocking loop when other messages arrive. show() & hide() do not work immediately as they are executed, rather they mark the window for closure. You do not see it close till the next time the main event loop sees it and visibly closes the window. So, is it possible/the case that the visual closure of the other window does not appear while the modal/blocking window is still present? And the closure happens after you exit the modal dialog's blocking loop?

          CJhaC 1 Reply Last reply
          0
          • JonBJ JonB

            @CJha
            This is perhaps a possibility, I'm not sure how modal blocking loop when other messages arrive. show() & hide() do not work immediately as they are executed, rather they mark the window for closure. You do not see it close till the next time the main event loop sees it and visibly closes the window. So, is it possible/the case that the visual closure of the other window does not appear while the modal/blocking window is still present? And the closure happens after you exit the modal dialog's blocking loop?

            CJhaC Offline
            CJhaC Offline
            CJha
            wrote on last edited by
            #5

            @JonB No, it does not close the non-modal window. I installed the event filter on non-modal window to see if the QEvent::Close event gets delivered or not and it never gets delivered to the non-modal window, not even after the modal window is closed. It appears that this->close() is completely ignored.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Windows can't be closed when a modal window is shown as per this comment.

              As to why - the idea of a modal window is that it "blocks" the execution of the rest of the ui while it is visible. That includes closing windows. If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.

              JonBJ CJhaC 2 Replies Last reply
              3
              • Chris KawaC Chris Kawa

                Windows can't be closed when a modal window is shown as per this comment.

                As to why - the idea of a modal window is that it "blocks" the execution of the rest of the ui while it is visible. That includes closing windows. If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Chris-Kawa said in Close non-modal window when a model window is opened?:

                Windows can't be closed when a modal window is shown as per this comment.

                Perfect, the sort of thing I wondered if it might be the case.

                If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.

                Sounds excellent to me!

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  Windows can't be closed when a modal window is shown as per this comment.

                  As to why - the idea of a modal window is that it "blocks" the execution of the rest of the ui while it is visible. That includes closing windows. If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.

                  CJhaC Offline
                  CJhaC Offline
                  CJha
                  wrote on last edited by
                  #8

                  @Chris-Kawa Ok, I understand and it does make sense.

                  In my case, my non-modal window has Qt::WindowStaysOnTopHint set, if the user opens a modal window that by chance happens to be placed under my non-modal window then the entire application gets blocked. Because of this reason I was trying to close my non-modal window if the new window that gets the focus is a modal window.

                  Chris KawaC 1 Reply Last reply
                  0
                  • CJhaC CJha

                    @Chris-Kawa Ok, I understand and it does make sense.

                    In my case, my non-modal window has Qt::WindowStaysOnTopHint set, if the user opens a modal window that by chance happens to be placed under my non-modal window then the entire application gets blocked. Because of this reason I was trying to close my non-modal window if the new window that gets the focus is a modal window.

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @CJha I would say you should rather remove the always on top flag before you show the modal window and restore it afterwards if needed.

                    CJhaC 1 Reply Last reply
                    1
                    • Chris KawaC Chris Kawa

                      @CJha I would say you should rather remove the always on top flag before you show the modal window and restore it afterwards if needed.

                      CJhaC Offline
                      CJhaC Offline
                      CJha
                      wrote on last edited by
                      #10

                      @Chris-Kawa Those are 2 completely different parts of the application, one does not have any idea of what the other part is showing that's why I was using the void QGuiApplication::focusWindowChanged(QWindow *focusWindow) signal to observe the change. I noticed that the this->hide() works for the non-modal window and it gets the job done for me.

                      1 Reply Last reply
                      0

                      • Login

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