Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Hiding viewer panel vs window maximization
QtWS25 Last Chance

Hiding viewer panel vs window maximization

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 76 Views
  • 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.
  • S Offline
    S Offline
    sapvi
    wrote on 10 Apr 2025, 08:55 last edited by
    #1

    Hi all.

    I have a specific situation where I do not understand how QT works and what is specifically wrong here.

    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?
      181bdbcb-c4b2-4f18-838f-81a2158bab73-image.png

    2. 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?

    1 Reply Last reply
    0
    • S SGaist moved this topic from General and Desktop 30 days ago
    • C Offline
      C Offline
      cinucen
      wrote 25 days ago last edited by cinucen
      #2

      When the state of self.was_maximized changed, the second if in adapt_viewer_panel is not working as expected. Adding self.was_maximized = not hide_viewer_panel to the else statement worked for me:

      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.was_maximized = not hide_viewer_panel
      
              self.updateGeometry()
              self.adjustSize()
              self.repaint()
      
              if not hide_viewer_panel and self.was_maximized:
                  self.showMaximized()
      
      1 Reply Last reply
      0

      1/2

      10 Apr 2025, 08:55

      • Login

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