[Python] Problems going between maximized and sidebar-only window

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have a specific situation where I do not understand how QT works and what is specifically wrong here. Below I describe two observable issues but I suppose that they follow from same underlying mistake in code.

    My goal:

    1. Have app with left and right side. Left always visible, right can be hidden.
    2. App can be maximized or any size above some specific minimum size when right panel is visible.
    3. When right panel is not visible, app has fixed width and cannot be maximized.
    4. By default, app is maximized on start of the app.
    5. If the app was maximized when right panel was hidden, the app goes to the fixed size defined for left-panel-only mode, but when right panel is shown again, app is maximized again.
    6. If the app was not maximized when right panel was hidden, it does not maximize when the right panel is shown again, instead just adapting based on the minimum size.

    Based on the description above, I made this MRE:

    import sys
    from PySide6.QtCore import QEvent
    from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QVBoxLayout,
    QPushButton,
    QHBoxLayout,
    QSizePolicy,
    )

    hide_viewer_panel = False


    class MainWindow(QMainWindow):
    def __init__(self):
    super().__init__()

    self.setWindowTitle("MRE Example")

    # Sidebar
    self.sidebar = QWidget(self)
    self.sidebar.setStyleSheet("background-color: lightgray;")
    self.sidebar.setMinimumWidth(200)
    self.sidebar.setMinimumHeight(500)

    # Viewer Panel
    self.viewer_panel = QWidget(self)
    self.viewer_panel.setStyleSheet("background-color: lightblue;")
    self.viewer_panel.setMinimumWidth(300)

    # Layout
    main_layout = QHBoxLayout()
    main_layout.addWidget(self.sidebar)
    main_layout.addWidget(self.viewer_panel)

    # Central widget
    central_widget = QWidget(self)
    central_widget.setLayout(main_layout)
    self.setCentralWidget(central_widget)

    # Sidebar button
    self.hide_button = QPushButton("Hide/Show Viewer Panel", self)
    self.hide_button.clicked.connect(self.hide_visu_panel)
    self.sidebar_layout = QVBoxLayout(self.sidebar)
    self.sidebar_layout.addWidget(self.hide_button)

    # Window maximized state tracking
    self.was_maximized = not hide_viewer_panel

    # Apply initial settings
    self.adapt_viewer_panel()

    def hide_visu_panel(self):
    global hide_viewer_panel
    hide_viewer_panel = not hide_viewer_panel
    self.adapt_viewer_panel()

    def adapt_viewer_panel(self):
    if hide_viewer_panel:
    self.setFixedWidth(200)
    self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)
    self.viewer_panel.hide()
    else:
    self.setMinimumWidth(1500)
    self.setMaximumWidth(16777215)
    self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
    self.viewer_panel.show()

    self.updateGeometry()
    self.adjustSize()
    self.repaint()

    if not hide_viewer_panel and self.was_maximized:
    self.showMaximized()

    def changeEvent(self, event):
    if event.type() == QEvent.WindowStateChange:
    if self.isMaximized():
    self.was_maximized = True
    else:
    self.was_maximized = False
    super().changeEvent(event)


    if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())


    To reproduce problems I describe below: just run this app, there is only one button, press it to hide the right panel and then press it again to show right panel. Do not move/change size of window.

    So, it does not work as I want / expect it to be.

    1. When I go directly from maximized window to left-panel-only, the title of the window gets messed up. It becomes wider than the app. This is automatically fixed when I move the window, but I do not understand why this happens and it also does not look well. How to get rid of this?

    [​IMG]

    1. When I try to go directly back to showing right panel, the window is not maximized. It seems that showMaximized() is actually called but is ignored :/ It works only on first call when the app is started, but after that it does not maximized the window anymore. It is still possible to maximize the window by pressing "maximize" button in the title of the window, so apparently the window can be maximized, Qt just chooses to ignore the call for whatever reason. How to fix this one also? What can be blocking Qt here? I understand that Qt does not immediately execute most of calls we pass it, but there should be some way to do something as simple as maximized a window?

    Continue reading...

Compartilhe esta Página