Populating Qtablewidget on the fly
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
you say but it's not working as expected, that does not indicate anything, it explains in detail what you get, is that an MCVE ?, it copies it and they throw me many errors
– eyllanesc
7 hours ago
what is does is it creates 10 columns when number of rows is 2. it's obvious that it creates columns based on the number of iterations inside the inner loop. I really can't figure out how to get it to work correctly
– tr3s
7 hours ago
but if I set the number of columns self.tblPipeline.setColumnCount(6) prior to the loop then it's good. Again, this is not what I need. I want to add columns the fly
– tr3s
7 hours ago
1 Answer
1
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_())
If you want to verify that the column is created you must use columnCount()
, modifying your code we get the following:
columnCount()
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_())
Thanks for this but I need to create columns on the fly because the number of columns should be dynamic. How can I check in PyQt if jth column is already created so the code will not create it again?
– tr3s
7 hours ago
you have an offset of 5-1 = 4, I think that you think you have the right solution, and then it does not make sense to ask here. I am creating the data in the fly.
– eyllanesc
6 hours ago
@tr3s see my update answer
– eyllanesc
6 hours ago
You got it. Thanks for your consideration and for providing 2 solutions
– tr3s
6 hours ago
Yes, thanks again. Upvoted but it says I have less than 15 reputations :)
– tr3s
6 hours ago
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.
show what you have tried
– eyllanesc
7 hours ago