简体   繁体   中英

PyQt4 - Adding multiple grid layouts to a tab widget

I'm using PyQt4, and Python 2.7. Initially I had a QGridLayout laid out with two table widgets, and some misc. stuff.

self.table_left = QtGui.QTableWidget(self)
self.table_right = QtGui.QTableWidget(self)
self.label_desired = QtGui.QLabel(self)
self.line = QtGui.QLineEdit(self)
self.label_current = QtGui.QLabel(self)

self.grid = QtGui.QGridLayout(self)
self.grid.addWidget(self.table_left, 0, 0, 20, 1)
self.grid.addWidget(self.table_right, 0, 1, 20, 1)
self.grid.addWidget(self.label_desired, 0, 2, 1, 3)
self.grid.addWidget(self.line, 1, 2, 1, 3)
self.grid.addWidget(self.label_current, 2, 2, 1, 3)

But now I want to use a for loop to generate these tables, and GridLayout, and add them to a tabwidget,

for k, value in enumerate(self.form_no):
    self.grid[k] = QtGui.QGridLayout(self)
    self.grid[k].addWidget(self.table_left[k], 0, 0, 20, 1)
    self.grid[k].addWidget(self.table_right[k], 0, 1, 20, 1)
    self.grid[k].addWidget(self.label_desired[k], 0, 2, 1, 3)
    self.grid[k].addWidget(self.line[k], 1, 2, 1, 3)
    self.grid[k].addWidget(self.label_current[k], 2, 2, 1, 3)
    self.tabwidget.addTab(self.grid[k], str(value))

When I tried this I got a type error. I looked around and couldnt find anything like a GridWidget. How can I get around this error to add the GridLayouts to a tab widget?

TypeError: arguments did not match any overloaded call:
  QTabWidget.addTab(QWidget, QString): argument 1 has unexpected type 'QGridLayout'
  QTabWidget.addTab(QWidget, QIcon, QString): argument 1 has unexpected type 'QGridLayout'

Add a QWidget to the tab instead of adding a QLayout : self.tabwidget.addTab(widget) . Then set the self.grid[k] to be the layout for this widget: widget.setLayout(self.grid[k]) .

widget = QtGui.QWidget()
widget.setLayout(self.grid[k])
tabwidget.addTab(widget, str(value))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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