How to add a row in a tableWidget PyQT?

sudobangbang picture sudobangbang · Jun 4, 2014 · Viewed 46.3k times · Source

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

Answer

Aleksandar picture Aleksandar · Jun 5, 2014

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)