Delayed Window State Read after Hide Gives Wrong Results Even in X11
-
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...
-
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...
@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/orisHidden()
if you need to detect that, perhaps to tell you whetherwindowState()
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
QWidget
s), which means to use it you will need to subclass yourQMainWindow
to your own class and overrideshowEvent()
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. -
What does your program actually output?
Unlike the previous report about reading windowState in singleShot
What previous report?
-
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.
-
@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.
-
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.