3

Sorry, my question may sound very stupid but Frankly i spend a lot of time on internet trying to figure out how to add a widgets to QMdiSubWindow, I mean a Multiple Widgets not just one Widget(this is very important for me .. I need my Sub window to contain a multiple widget not a single widget).

for example i want to add this widgets and layouts to My Sub Window:

QVbox which contains a QlineEdit and Push Button, And QHbox which contain a a Push Button ...

it doesn't matter if you show me how i could do it using the above example, what really matter is to show me how no matter what example you use

Note : Please Use OOP and Python not c++

3
  • What have you tried? Did you try adding a generic instance of QWidget to the QMdiSubWindow, then adding a layout to that generic QWidget then and putting all of your other widgets inside that layout? Commented Jun 2, 2016 at 3:30
  • I am not sure that I am following you ??
    – Ayyoub
    Commented Jun 3, 2016 at 21:43
  • in that case, I've explained in more detail in the answer below. I think this will solve your problem. Commented Jun 4, 2016 at 1:18

1 Answer 1

7

The hierarchy of widgets and layouts should always follow

widget -> layout -> widget -> layout -> widget -> ...

where each widget can only have one layout, but each layout can contain multiple widgets (note that for simplicity the above only shows one widget per layout).

So, what you need to do is create a widget which contains a layout, and that layout contains the QPushButton andQLineEdit`. For example:

# construct the top level widget
widget = QWidget()
# construct the top level layout
layout = QVBoxLayout(widget)

# create the widgets to add to the layout
button = QPushButton('My Button')
lineedit = QLineEdit()

# add the widgets to the layout
layout.addWidget(button)
layout.addWidget(lineedit)

# set the layout on the widget
widget.setLayout(layout)
# we now have a single widget that contains a layout and 2 widgets

This allows you to effectively encapsulate multiple widgets inside a single widget (and is how a lot of the more complex Qt widgets are created internally, for instance the QSpinBox). So if you want another complex widget inside the layout above, you could again make another widget2 = QWidget(), create a layout (for instance layout2 = QHBoxLayout(widget2)), and then add multiple widgets to it. Once done, add the widget2 widget to the original layout (layout.addWidget(widget2)).

Hopefully you can now see how to construct a single complex widget from an arbitrary number of child widgets and layouts.

At this point you can now set the single widget to an existing QMdiSubWIndow

# now add this widget to the QMdiSubWindow
mdisubwindow.setWidget(widget)

or alternatively, call the convenience function on the QMdiArea to create a new QMdiSubWindow with the widget:

mdisubwindow = mdiarea.addSubWindow(widget)

Note: For your specific example, you don't actually need to construct widget2 to encapsulate the QHBoxLayout. You can add a QHBoxLayout (layout2 in my rough example above) directly to the original layout by calling layout.addLayout(layout2). However, this is more of a special case, and the general principle of encapsulation above of alternating widgets and layouts is more generalisable once you start making your own widget classes in order to re-use code.

2
  • Thanks so mush that worked very well, from your code my issue was that I am trying to add addLayout instead of addWidget
    – Ayyoub
    Commented Jun 4, 2016 at 11:56
  • 1
    But a layout can also contain other layouts. No need to put a widget between. (QLayout is a QWidget too)
    – testo
    Commented Mar 22, 2021 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.