pyQt QLayout问题

2024-04-25 15:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

这让我有点疯狂。希望有人能帮我弄清楚。运行以下代码将导致第一个print语句是一个包含一个元素的列表QVBoxLayout对象。我将两个对象设置为layout为什么我只得到一个?在

第二个print语句给出了两个对象QHBoxLayout和{}。QPushButton不是layout的孩子吗?在

我希望layout.children()给我两个对象QPushButton和{} 和self.children()给我一个对象QHBoxLayout。我错过了什么?在

from PySide.QtGui import *
import sys

class Main(QWidget):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout = QHBoxLayout(self)
        layout.addWidget(QPushButton("foo"))

        layout.addLayout(QVBoxLayout())

        print layout.children()
        print self.children()

app = QApplication([])
main = Main()
main.show()
sys.exit(app.exec_())

Tags: 对象importselfappinitmainsys语句
1条回答
网友
1楼 · 发布于 2024-04-25 15:20:33

我想来自documentation的注释已经足够清楚地解释了这一点:

Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

相关问题 更多 >