Frage

I'm developing a tool in PyQt4 which edits config files. It changes the GUI every time a file is loaded. The editor GUI is a QTabWidget containing as many tabs as the number of chapters in the loaded file. 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.

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

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.

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?

War es hilfreich?

Lösung

I got answer on a different channel, thanks to the guys on #pyqt on freenode. The problem is that QScrollArea needs a QWidget set by setWidget(), and that should contain the 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. :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top