17

I am currently working on a widget that was designed in Qt Designer. I am having trouble with the syntax / overall concept of trying to add a row to a Qtable in PyQT. There is no method, which I have yet found to dynamically add rows. Any suggestions would be helpful.

Regards

sudobangbang
  • 1,406
  • 10
  • 32
  • 55

4 Answers4

62

You can add empty row and later populate all columns. This is how to insert row under all other rows:

rowPosition = self.table.rowCount()
table.insertRow(rowPosition)

after that you have empty row that you can populate like this for example( if you have 3 columns):

table.setItem(rowPosition , 0, QtGui.QTableWidgetItem("text1"))
table.setItem(rowPosition , 1, QtGui.QTableWidgetItem("text2"))
table.setItem(rowPosition , 2, QtGui.QTableWidgetItem("text3"))

You can also insert row at some other position (not necessary at the end of table)

Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • 4
    For some reason I had to also make table.setColumnCount(3) to make the table contents visible. (might be because I am using PyQt5) – Niko Föhr Dec 12 '16 at 08:56
2

It is somewhat peculiar, I've found. To insert a row, you have to follow something similar to this:

tableWidget = QTableWidget()
currentRowCount = tableWidget.rowCount() #necessary even when there are no rows in the table
tableWidget.insertRow(currentRowCount, 0, QTableWidgetItem("Some text"))

To clarify the last line of code, the first parameter of insertRow() is the current row, the second one is current column (remember it's always 0-based), and the third must almost always be of type QTableWidgetItem).

Bo Milanovich
  • 7,995
  • 9
  • 44
  • 61
  • 1
    Not working...TypeError: QTableWidget.insertRow(int): too many arguments.... Please edit the answer... – Lokesh Sanapalli Mar 19 '17 at 14:06
  • you have do like `tableWidget.setItem(currentRowCount, 0, QTableWidgetItem("Some text"))` and **NOT** `tableWidget.insertRow(currentRowCount, 0, QTableWidgetItem("Some text"))` – Sid133 Jan 10 '20 at 09:01
2
def add_guest(self):
    rowPosition = self.tableWidget.rowCount()
    self.tableWidget.insertRow(rowPosition)
    guest_name = self.lineEdit.text()
    guest_email = self.lineEdit_2.text()
    numcols = self.tableWidget.columnCount()
    numrows = self.tableWidget.rowCount()           
    self.tableWidget.setRowCount(numrows)
    self.tableWidget.setColumnCount(numcols)           
    self.tableWidget.setItem(numrows -1,0,QtGui.QTableWidgetItem(guest_name))
    self.tableWidget.setItem(numrows -1,1,QtGui.QTableWidgetItem(guest_email))
    print "guest added"         

This is how i got it done for my event organisation application

1

You can use this function

def table_appender(widget, *args):

    def set_columns(len, pos):
        if pos == len-1:
            widget.setItem(widget.rowCount()-1, pos, QTableWidgetItem(args[pos]))
        else:
            widget.setItem(widget.rowCount()-1, pos, QTableWidgetItem(args[pos]))
            set_columns(len, pos+1)
    widget.insertRow(widget.rowCount())
    set_columns(widget.columnCount(), 0)
saman koushki
  • 111
  • 1
  • 6