这让我有点抓狂。希望有人能帮我把这事说清楚。运行以下代码会使第一个打印语句成为一个只有一个元素的列表,即QVBoxLayout对象。我给layout设置了两个对象,为什么只有一个呢?
第二个print语句给出了两个对象QHBoxLayout和QPushButton。QPushButton不是layout的孩子吗
我希望layout.children()给我两个对象QPushButton和QVBoxLayout,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_())发布于 2012-02-18 12:20:15
我想documentation的说明已经很清楚地解释了这一点:
注意:布局中的小部件是安装布局的小部件的子项,而不是布局本身的子项。小部件只能将其他小部件作为父部件,而不能将布局作为父部件。
https://stackoverflow.com/questions/9338272
复制相似问题