PyQt4对象不显示

时间:2012-12-09 00:49:52

标签: python pyqt tabwidget

我正在使用PyQt4开发一个编辑配置文件的工具。每次加载文件时它都会更改GUI。编辑器GUI是一个QTabWidget,包含与加载文件中章节数一样多的选项卡。在每个选项卡上,它具有与加载文件中的子部分对应的QGroupBox,最后这些组保存与实际配置参数和值对应的QLineEdits。

所有这些都是从一本大字典中构建的,并且可以正常工作:

  • 所有标签均正确显示
  • 所有组都正确显示所有组
  • 但是 LineEdits仅显示在第一个标签的第一个组中

也为其他组创建LineEdit对象,我打印它们并且它们都指向不同的内存位置 - 仍然不显示它们。

以下是代码的相应部分:

while self.tabWidget.widget(0):
    self.tabWidget.removeTab(0)
for i in data['tabList']:
    self.log("Adding tab: '%s'" % i, DEBUG)
    self.data['tabDict'][i]['scrollarea'] = QScrollArea()
    self.data['tabDict'][i]['vbox'] = QVBoxLayout()
    for j in self.data['tabDict'][i]['groupList']:
        self.log("Adding group: '%s'" % j, DEBUG)
        self.data['tabDict'][i]['groupDict'][j]['groupbox'] = QGroupBox(j)
        self.data['tabDict'][i]['groupDict'][j]['formlo'] = QFormLayout()
        print self.data['tabDict'][i]['groupDict'][j]['formlo']
        for k in self.data['tabDict'][i]['groupDict'][j]['fields']:
            self.log("Adding field: '%s'" % k['name'])
            k['lineedit']  = QLineEdit(k['value'])
            k['lineedit'].setToolTip('<b>Type:</b> %s<br><b>TSDB path:</b> %s<br><b>Line:</b> %d<br><b>Comment:</b> %s' % (k['type'],k['path'],k['row'], k['comment']))
            self.data['tabDict'][i]['groupDict'][j]['formlo'].addRow(k['name'], k['lineedit'])
        self.data['tabDict'][i]['groupDict'][j]['groupbox'].setLayout(self.data['tabDict'][i]['groupDict'][j]['formlo'])
        self.data['tabDict'][i]['vbox'].addWidget(self.data['tabDict'][i]['groupDict'][j]['groupbox'])
    self.data['tabDict'][i]['scrollarea'].setLayout(self.data['tabDict'][i]['vbox'])
    self.tabWidget.addTab(self.data['tabDict'][i]['scrollarea'], i)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我在另一个频道上得到了答案,感谢freenode上的#pyqt上的人们。问题是QScrollArea需要QWidget设置setWidget(),并且应该包含QVBoxLayout

这是固定代码:

    while self.tabWidget.widget(0):
        self.tabWidget.removeTab(0)
    for i in data['tabList']:
        self.log("Adding tab: '%s'" % i, DEBUG)
        self.data['tabDict'][i]['scrollarea'] = QScrollArea()
        self.data['tabDict'][i]['scrollarea'].setWidgetResizable(True)
        self.data['tabDict'][i]['widget'] = QWidget()
        self.data['tabDict'][i]['vbox'] = QVBoxLayout()
        for j in self.data['tabDict'][i]['groupList']:
            self.log("Adding group: '%s'" % j, DEBUG)
            self.data['tabDict'][i]['groupDict'][j]['groupbox'] = QGroupBox(j)
            self.data['tabDict'][i]['groupDict'][j]['formlo'] = QFormLayout()
            print self.data['tabDict'][i]['groupDict'][j]['formlo']
            for k in self.data['tabDict'][i]['groupDict'][j]['fields']:
                self.log("Adding field: '%s'" % k['name'])
                k['lineedit']  = QLineEdit(k['value'])
                k['lineedit'].setToolTip('<b>Type:</b> %s<br><b>TSDB path:</b> %s<br><b>Line:</b> %d<br><b>Comment:</b> %s' % (k['type'],k['path'],k['row'], k['comment']))
                self.data['tabDict'][i]['groupDict'][j]['formlo'].addRow(k['name'], k['lineedit'])
            self.data['tabDict'][i]['groupDict'][j]['groupbox'].setLayout(self.data['tabDict'][i]['groupDict'][j]['formlo'])
            self.data['tabDict'][i]['vbox'].addWidget(self.data['tabDict'][i]['groupDict'][j]['groupbox'])
        self.data['tabDict'][i]['widget'].setLayout(self.data['tabDict'][i]['vbox'])
        self.data['tabDict'][i]['scrollarea'].setWidget(self.data['tabDict'][i]['widget'])
        self.tabWidget.addTab(self.data['tabDict'][i]['scrollarea'], i)

虽然导致第一个代码的奇怪行为,但仍然是一个悬而未决的问题。 :)