Find what rows are visible in a scrolling table
-
wrote on 29 Apr 2020, 03:33 last edited by
I would appreciate if somebody can point me in the right direction. I have a table with 1 column and 60 rows of which 24 rows are visible at any one time. This table has 24 checkboxes on the side, one for each visible row. When a person clicks on the checkbox how can I find out which row was selected? If I can find out the top most row that is visible I can find out everything else. Is there any function that will tell me that say, 7th row is the top most visible row at this time? Thanks.
-
Hi and welcome to devnet,
No there's no such method. However you can use visualRect to achieve that.
-
wrote on 30 Apr 2020, 00:39 last edited by
SGaist: Thanks but I just started working with qt and visualRect is well beyond my grasp at this juncture.
Denni: I took your advice and added the checkboxes into the table using the code from the answer section of this post:
https://stackoverflow.com/questions/48057638/how-should-i-connect-checkbox-clicked-signals-in-table-widgets-in-pyqt5Now how do I center this checkbox in the table cell? Thanks.
-
wrote on 30 Apr 2020, 22:50 last edited by
My code for inserting the checkbox in the table looks like this. The checkbox comes out as left-justified and I would like to center it. Thanks.
for i in range(self.tableWidget.rowCount()): ch = QtWidgets.QCheckBox(parent=self.tableWidget) ch.clicked.connect(lambda checked, row=0, col=i: self.onStateChanged(checked, row, col)) self.tableWidget.setCellWidget(i, 0, ch)
-
My code for inserting the checkbox in the table looks like this. The checkbox comes out as left-justified and I would like to center it. Thanks.
for i in range(self.tableWidget.rowCount()): ch = QtWidgets.QCheckBox(parent=self.tableWidget) ch.clicked.connect(lambda checked, row=0, col=i: self.onStateChanged(checked, row, col)) self.tableWidget.setCellWidget(i, 0, ch)
wrote on 1 May 2020, 15:17 last edited by JonB 5 Jan 2020, 15:50@ACollins said in Find what rows are visible in a scrolling table:
ch.clicked.connect(lambda checked, row=0, col=i: self.onStateChanged(checked, row, col))
Before you overlook it, I presume you intend:
ch.clicked.connect(lambda checked, row=i, col=0: self.onStateChanged(checked, row, col))
(
row
/col
values swapped).This isn't the best way to do things [ I'm looking at you, @VRonin ;-) ], but from where you are now presumably you can always:
w = QtWidgets.QWidget(parent=self.tableWidget) w.setLayout(QtWidgets.QHBoxLayout()) ch = QtWidgets.QCheckBox() w.layout().addWidget(ch) ch.clicked.connect(lambda checked, row=i, col=0: self.onStateChanged(checked, row, col)) self.tableWidget.setCellWidget(i, 0, w)
i.e. just put the checkbox in a horizontal layout.
I'm not sure why in the code you copied from the author goes for a
setCellWidget(QCheckBox)
at all. Wouldn't a blankQTableWidgetItem
withsetFlags(... or Qt.ItemIsUserCheckable)
suffice? -
wrote on 1 May 2020, 22:32 last edited by
Thank you very much JonB & Denni. All good and working perfectly!
Denni: I wasn't sure what MUC is. I have been away from programming for 15+ years so this lingo is all new. I think I will read the FAQ.
1/6