如何将小部件放在 PyQt QMdiSubWindow 中 [英] how to put widgets inside a PyQt QMdiSubWindow

查看:74
本文介绍了如何将小部件放在 PyQt QMdiSubWindow 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我的问题可能听起来很愚蠢,但坦率地说,我花了很多时间在互联网上试图弄清楚如何将小部件s添加到 QMdiSubWindow,我的意思是多个小部件 不仅仅是一个小部件(这对我来说非常重要......我需要我的子窗口包含多个小部件而不是单个小部件).

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:

包含 QlineEdit 和 Push Button 的 QVbox,以及包含 Push Button 的 QHbox ...

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

注意:请使用 OOP 和 Python 而不是 C++

Note : Please Use OOP and Python not c++

推荐答案

小部件和布局的层次结构应始终遵循

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).

因此,您需要做的是创建一个包含布局的小部件,该布局包含 QPushButton 和QLineEdit`.例如:

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

这使您可以将多个小部件有效地封装在单个小部件中(以及如何在内部创建许多更复杂的 Qt 小部件,例如 QSpinBox).因此,如果您想要在上面的布局中使用另一个复杂的小部件,您可以再次制作另一个 widget2 = QWidget(),创建一个布局(例如 layout2 = QHBoxLayout(widget2)),然后向其中添加多个小部件.完成后,将 widget2 小部件添加到原始布局 (layout.addWidget(widget2)).

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.

此时您可以将单个小部件设置为现有的QMdiSubWIndow

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

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

或者,调用 QMdiArea 上的便利函数以使用小部件创建一个新的 QMdiSubWindow:

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

mdisubwindow = mdiarea.addSubWindow(widget)

注意:对于您的具体示例,您实际上不需要构造 widget2 来封装 QHBoxLayout.您可以通过调用 layout.addLayout(layout2)QHBoxLayout(在我上面的粗略示例中为 layout2)直接添加到原始布局.然而,这更像是一种特殊情况,一旦您开始制作自己的小部件类以便重用代码,上面交替小部件和布局的封装的一般原则就更具普遍性.

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.

这篇关于如何将小部件放在 PyQt QMdiSubWindow 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆