Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-91762

Adding QMainWindow to layout does not work if parent is set

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.15.1, 6.0.2
    • Widgets: Layout
    • None
    • Linux Qt 5.15.1, Pyside2 (5.15.0), PySide6 (6.0.2)
    • Linux/X11

    Description

      I want to add a QMainWindow to a QDialog. In order to do that, I set a layout, add the QMainWindow to that layout and then put my center widgets and dockWidgets to it. This code explains the setup (I have the same problem in my C++ code, so this is not Python related):

      import sys
      import PySide2
      from PySide2.QtWidgets import QApplication, QDialog, QVBoxLayout, QMainWindow, QLabel, \
          QDockWidget
      
      
      class Dialog1(QDialog):
          def __init__(self, parent=None):
              super(Dialog1, self).__init__(parent)
              layout = QVBoxLayout()
              self.setLayout(layout)
      
              self.mw = QMainWindow(self)
              layout.addWidget(self.mw)
              self.mw.setCentralWidget(QLabel("CentralWidget"))
      
              dock = QDockWidget()
              dock.layout().addWidget(QLabel("DockWidget"))
              self.mw.addDockWidget(PySide2.QtCore.Qt.DockWidgetArea.RightDockWidgetArea, dock)
      
      
      app = QApplication(sys.argv)
      ui = Dialog1()
      ui.show()
      sys.exit(app.exec_())
      
      

      The result (after resizing) is this:

      When I don't set the parent or set it to null:

      self.mw = QMainWindow()
      

       or

      self.mw = QMainWindow(None)
      

      or 

      self.mw = QMainWindow(self)
      self.mw.setParent(None)
      
      

      I get as expected:

      In my C++ code, the `QMainWindow` construction and the `addWidget` is done by the generated code from the UI file. My current workaround is to do something like this after `setupUi`

      ui.layout().removeWidget(ui.mw)
      ui.mw.setParent(None)
      ui.layout().addWidget(ui.mw)
      

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            trinklin Thomas Rinklin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes