简体   繁体   English

PyQt4对象不显示

[英]PyQt4 object does not display

I'm developing a tool in PyQt4 which edits config files. 我正在PyQt4中开发一个可编辑配置文件的工具。 It changes the GUI every time a file is loaded. 每次加载文件时都会更改GUI。 The editor GUI is a QTabWidget containing as many tabs as the number of chapters in the loaded file. 编辑器GUI是QTabWidget,其中包含与所加载文件中的章节数一样多的选项卡。 On each tab, it has QGroupBoxes corresponding to the subsections in the loaded file and finally the groups hold QLineEdits which correspond to the actual config parameters and values. 在每个选项卡上,它都有QGroupBoxes对应于已加载文件中的小节,最后,这些组包含QLineEdits,它们对应于实际的配置参数和值。

All this is built up from a large dictionary and works fine up to a point: 所有这些都是通过大型词典构建的,并且可以正常工作:

  • all tabs are displayed correctly 所有选项卡显示正确
  • all groups are displayed correctly in all tabs 所有组均正确显示在所有选项卡中
  • BUT LineEdits are displayed only in the first group of the first tab 但是 LineEdits仅显示在第一个选项卡的第一组中

LineEdit objects are created for the other groups as well, I printed them and they all point to a different memory location - still they are not displayed. LineEdit对象也为其他组创建,我将它们打印出来,它们都指向不同的存储位置-仍然不显示。

Here is the corresponding part of the code: 这是代码的相应部分:

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)

What am I missing here? 我在这里想念什么?

I got answer on a different channel, thanks to the guys on #pyqt on freenode. 感谢freenode上#pyqt上的人,我在另一个频道上得到了答案。 The problem is that QScrollArea needs a QWidget set by setWidget() , and that should contain the QVBoxLayout . 问题是QScrollArea需要一个由setWidget()设置的QWidget ,并且应该包含QVBoxLayout

Here is the fixed code: 这是固定代码:

    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)

It is still an open question though that what resulted the weird behavior of the first code. 尽管是什么导致了第一个代码的怪异行为,仍然是一个悬而未决的问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM