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. Delayed Window State Read after Hide Gives Wrong Results Even in X11
Forum Updated to NodeBB v4.3 + New Features

Delayed Window State Read after Hide Gives Wrong Results Even in X11

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.1k Views 1 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.
  • J Offline
    J Offline
    johnzhou721
    wrote last edited by
    #1

    See reproducer below.

    from PySide6.QtWidgets import QApplication, QMainWindow
    from PySide6.QtCore import Qt, QTimer
    
    app = QApplication([])
    
    window = QMainWindow()
    
    # Show maximized
    window.showMaximized()
    print("After showMaximized:", window.windowState())
    
    # Hide the window
    window.hide()
    
    # Query window state 0.1 seconds later
    def query_state():
        print("0.1 sec after hide:", window.windowState())
    
    QTimer.singleShot(100, query_state)  # 100 ms = 0.1 sec
    
    app.exec()
    

    Here, we first show a window maximized, and then hide it; after 0.1 seconds, we get the state of the window, which, as far as I understand, should remain unchanged even if I hide.

    However, after 100ms I'm getting that there's no state at all. Unlike the previous report about reading windowState in singleShot, this is also happening under X11.

    Not sure if singleShot is the culprit here because when using third-party qasync and asyncio.sleep this also happens, so I'm wondering if the window state when hidden is supposed to be reliable at all...

    JonBJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote last edited by
      #2

      What does your program actually output?

      Unlike the previous report about reading windowState in singleShot

      What previous report?

      J 1 Reply Last reply
      0
      • J johnzhou721

        See reproducer below.

        from PySide6.QtWidgets import QApplication, QMainWindow
        from PySide6.QtCore import Qt, QTimer
        
        app = QApplication([])
        
        window = QMainWindow()
        
        # Show maximized
        window.showMaximized()
        print("After showMaximized:", window.windowState())
        
        # Hide the window
        window.hide()
        
        # Query window state 0.1 seconds later
        def query_state():
            print("0.1 sec after hide:", window.windowState())
        
        QTimer.singleShot(100, query_state)  # 100 ms = 0.1 sec
        
        app.exec()
        

        Here, we first show a window maximized, and then hide it; after 0.1 seconds, we get the state of the window, which, as far as I understand, should remain unchanged even if I hide.

        However, after 100ms I'm getting that there's no state at all. Unlike the previous report about reading windowState in singleShot, this is also happening under X11.

        Not sure if singleShot is the culprit here because when using third-party qasync and asyncio.sleep this also happens, so I'm wondering if the window state when hidden is supposed to be reliable at all...

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote last edited by
        #3

        @johnzhou721 said in Delayed Window State Read after Hide Gives Wrong Results Even in X11:

        and then hide it; after 0.1 seconds, we get the state of the window, which, as far as I understand, should remain unchanged even if I hide.

        so I'm wondering if the window state when hidden is supposed to be reliable at all...

        I have tried your code under Ubuntu 24.04, GNOME, Xorg, Qt ~6.4 and get the same results as you. I would imagine that a "hidden" window is not supposed to return any of the states in https://doc.qt.io/qt-6/qt.html#WindowState-enum for its "size" when it is "hidden/not shown", that does not seem unreasonable to me. Use isVisible() and/or isHidden() if you need to detect that, perhaps to tell you whether windowState() is currently useful?

        For this, and some of your other tests/questions, you may want to look into acting on void QWidget::showEvent(QShowEvent *event). This is a Qt event (inherited by all QWidgets), which means to use it you will need to subclass your QMainWindow to your own class and override showEvent() there. That event function is called when Qt actually shows a widget/window and you may find that e.g. the windowstate is more accurately portrayed at that point and/or you may want it to see what actually happens at what instant.

        1 Reply Last reply
        0
        • C ChrisW67

          What does your program actually output?

          Unlike the previous report about reading windowState in singleShot

          What previous report?

          J Offline
          J Offline
          johnzhou721
          wrote last edited by
          #4

          @ChrisW67 Oh, a previous forum post of mine. Don't worry.

          @JonB So... this is expected? Ok, then, I'd need to keep track of the state myself. If you're really sure about this, I'm gonna mark this as solved. Thanks!

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote last edited by
            #5

            The problem I have with your exampele problems is that you are building up this queue of events before executing the app.exec(). I think the more correct way to accomplish your goals is to subclass you visual windows (QMainWindow) and put all its initialization code in the overridden constructor. I think that would get rid of some of the problems you seem to be having.

            I light my way forward with the fires of all the bridges I've burned behind me.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              johnzhou721
              wrote last edited by
              #6

              @Kent-Dorfman Nice to know! Sorry, just trying to get a bunch of random commands together to see if this is actually a series of bugs in Qt or my operational error. I will try your suggestion though.

              @JonB interesting insight. thanks.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                johnzhou721
                wrote last edited by
                #7

                Marking as solved. The solution was to cache the state before hide and restore when shown.

                I mean the setter docs (https://doc.qt.io/qt-6/qwidget.html#setWindowState) said any sets to window state will apply after show... not sure what is going on here.

                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