I am attempting to populate a QTableWidget by adding rows, this is done by the user clicking on the add button:

QTableWidget

I have the following code to try and insert rows but nothing is happening:

def addButton_clicked():
    # objectName for add button = addButton
    # objectName for QTableWidget = tableWidget
    model = dockwidget.tableWidget
    rowCount = self.model.rowCount()
    self.model.insertRows(rowCount, 1)

add_button = self.dockwidget.addButton
add_button.clicked.connect(addButton_clicked)

The Group and Order columns were created from Qt Designer. I am trying to create rows in which the user can select a group from the ToC (in the Group column) and assign an integer (in the Order column). Is something like this possible?

I am open to other ideas.


The following code from @WKT creates a table with columns and x number of rows. I can populate the table with the names of the groups in the ToC. However, I only want to populate the first column. The code I run at the moment populates all columns. Here is the code:

qTable = self.dockwidget.tableWidget
data = []
group1 = root.findGroup('Group1')
group2 = root.findGroup('Group2')
for child in group1.children():
    data.append(child.name())
for child in group2.children():
    data.append(child.name())
nb_row = len(data)
nb_col = 2
qTable.setRowCount(nb_row)
qTable.setColumnCount(nb_col)

for row in range(nb_row):
    for col in range(nb_col):
        item = QTableWidgetItem(str(data[row]))
        qTable.setItem(row,col,item)

qTable.setHorizontalHeaderLabels([u'Column1',u'Column2'])
qTable.resizeColumnsToContents()

QTableWidget

up vote 8 down vote accepted

Let me share how it works for me:

self.setWindowTitle(title)

#data = 'Recordset back from postgis'
nb_row = len(data)
nb_col = 2

qTable.setRowCount(nb_row)
qTable.setColumnCount(nb_col)

for row in range (nb_row):
    for col in range(nb_col):
        item = QTableWidgetItem(str(data[row][col]))
        qTable.setItem(row,col,item)

qTable.setHorizontalHeaderLabels([u'Column1',u'Column2'])

qTable.resizeColumnsToContents()
  • Thank you for sharing, so you basically just immediately populate the table with rows/columns? I will come back to this in a couple of days and let you know how I get on :) – Joseph May 13 '16 at 15:09
  • I use it in a generic Dialog to show results of a DB query and let the user pick a row. Run fine. Any problem, let us know.... – WKT May 13 '16 at 15:49
  • Yes, sorry you are correct. Tried to delete the comment before you could read it ;). – Joseph May 16 '16 at 13:24
  • Thank you, got it working. The last hurdle is to populate the first column with a set of group names from the ToC. I've updated my question =) – Joseph May 16 '16 at 13:46
  • Not sure to understand but I think that the "data" var need, in your case, to look like [["group1", "child1"],["group1", "child2",[etc...]... – WKT May 16 '16 at 15:10

Found a way, basically you just need to specify the column when populating the QTableWidget (mentioned in comment):

qTable = self.dockwidget.tableWidget
data = []
group1 = root.findGroup('Group1')
group2 = root.findGroup('Group2')
for child in group1.children():
    data.append(child.name())
for child in group2.children():
    data.append(child.name())
nb_row = len(data)
nb_col = 2
qTable.setRowCount(nb_row)
qTable.setColumnCount(nb_col)

for row in range(nb_row):
    # Add following line to only populate first column
    for col in [0]:
        item = QTableWidgetItem(str(data[row]))
        qTable.setItem(row,col,item)

qTable.setHorizontalHeaderLabels([u'Column1',u'Column2'])
qTable.resizeColumnsToContents()

QTableWidget

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.