Search code examples
pyqt5qtablewidget

Populating Qtablewidget on the fly


How do you code this in PyQt5 so that the rows are created individually in the outer loop and the columns are created inside the inner loop? I'm aware that I can just set the number of rows and columns before the loop, but I need to create the rows and columns on the fly.

I'm having trouble doing it so that the table will look like below. If number of rows is 2 then the table should contain

1 2 3 4 5
  1 2 3 4 5

If number of rows is 3 then

1 2 3 4 5
  1 2 3 4 5
    1 2 3 4 5

What I currently have is

i = 0
while i < numrows:
  self.tblPipeline.insertRow(i)

  j = i
  while j < 5 + i:
    self.tblPipeline.insertColumn(j)
    self.tblPipeline.setItem(i, j, QTableWidgetItem(str(j)))

    j = j + 1

  i = i + 1

but it's not working as expected

0          1 2 3 4
 1 2 3 4 5

Thanks


Solution

  • You are adding the columns unnecessarily, in the following example I show how to do it.

    from PyQt5 import QtCore, QtWidgets, QtGui
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.tblPipeline = QtWidgets.QTableWidget()
            self.setCentralWidget(self.tblPipeline)
    
            numrows = 2
    
            self.tblPipeline.setColumnCount(5-1)
            for i in range(numrows):
                self.tblPipeline.insertColumn(self.tblPipeline.columnCount())
                self.tblPipeline.insertRow(i)
                for k, j in enumerate(range(i, self.tblPipeline.columnCount())):
                    self.tblPipeline.setItem(i, j, QtWidgets.QTableWidgetItem(str(k+1)))
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    
    • numrows = 2

    enter image description here

    • numrows = 3

    enter image description here


    If you want to verify that the column is created you must use columnCount(), modifying your code we get the following:

    from PyQt5 import QtCore, QtWidgets, QtGui
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.tblPipeline = QtWidgets.QTableWidget()
            self.setCentralWidget(self.tblPipeline)
    
            numrows = 3
    
            i = 0
    
            while i < numrows:
                self.tblPipeline.insertRow(i)
                j = i
                counter = 1
                while j < 5 + i:
                    if j >= self.tblPipeline.columnCount():
                        self.tblPipeline.insertColumn(j)
                    self.tblPipeline.setItem(i, j, QtWidgets.QTableWidgetItem(str(counter)))
                    counter += 1
                    j += 1
                i += 1
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())