Posts: 11
Threads: 3
Joined: Apr 2025
Hi, guys...
I have a qtablewidget with 3 columns that shows registers from my database.
Each row has a checkbox in the 3rd column.
Im trying to retrieve the content in the first cell that has the checkbox checked.
Here is the code :
items = []
for i in range(self.tblparc.rowCount()):
item = self.tblparc.item(i,2)
if item.checkState() == QtCore.Qt.Checked:
items.append(self.tblparc.itemAt(i,0)) Is it right ? How can I retrieve the contents ?
Thanks
Posts: 1,032
Threads: 16
Joined: Dec 2016
Do you want the text?
items.append(self.tblparc.itemAt(i,0).text())
Posts: 11
Threads: 3
Joined: Apr 2025
Yes...I want the text. Tried here and don't understand why the reply repeats the first one that he find.
Posts: 1,032
Threads: 16
Joined: Dec 2016
An Example
from PyQt5.QtWidgets import (QMainWindow, QApplication, QCheckBox,
QTableWidget, QTableWidgetItem)
from PyQt5.QtCore import Qt
class mainWin(QMainWindow):
def __init__(self, parent=None):
super(mainWin, self).__init__(parent)
self.setupUI()
def setupUI(self):
self.setGeometry(0, 0, 800, 600)
self.setContentsMargins(10, 5, 10, 5)
self.lb = QTableWidget(20, 2)
self.setCentralWidget(self.lb)
self.make_table()
self.get_checked_items()
def make_table(self):
for row in range(self.lb.rowCount()):
self.lb.setItem(row, 0, QTableWidgetItem(f"cell {row}"))
self.lb.setCellWidget(row, 1, QCheckBox())
### check every 3rd for testing
for row in range(0, 19, 3):
self.lb.cellWidget(row, 1).setChecked(True)
def get_checked_items(self):
items = []
for row in range(self.lb.rowCount()):
if self.lb.cellWidget(row, 1).checkState() == Qt.Checked:
items.append(self.lb.item(row, 0).text())
print(f"checked items: {items}")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = mainWin()
win.setWindowTitle("Example")
win.show()
sys.exit(app.exec_()) Output: checked items: ['cell 0', 'cell 3', 'cell 6', 'cell 9', 'cell 12', 'cell 15', 'cell 18']
Posts: 11
Threads: 3
Joined: Apr 2025
You've got right to the point...my code was:
checkbox_item = QTableWidgetItem()
checkbox_item.setFlags(checkbox_item.flags() | Qt.ItemIsUserCheckable)
checkbox_item.setCheckState(Qt.Unchecked)
self.tblparc.setItem(row,2,checkbox_item) And I was getting "AttributeError: 'NoneType' object has no attribute 'checkState'"
Danke schon...
Posts: 1,032
Threads: 16
Joined: Dec 2016
Use QTableWidgetItem for text and QCheckBox as CellWidget
Posts: 1,032
Threads: 16
Joined: Dec 2016
Apr-07-2025, 04:09 PM
(This post was last modified: Apr-07-2025, 04:09 PM by Axel_Erfurt.)
But it can also work with a checkable QTableWidgetItem
from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton,
QTableWidget, QTableWidgetItem, QMessageBox)
from PyQt5.QtCore import Qt
class mainWin(QMainWindow):
def __init__(self, parent=None):
super(mainWin, self).__init__(parent)
self.setupUI()
def setupUI(self):
self.setGeometry(0, 0, 800, 600)
self.setContentsMargins(10, 5, 10, 5)
tbar = self.addToolBar("Toolbar")
btn = QPushButton("check selected", clicked=self.get_checked_items)
tbar.addWidget(btn)
self.lb = QTableWidget(20, 2)
self.setCentralWidget(self.lb)
self.make_table()
def make_table(self):
for row in range(self.lb.rowCount()):
self.lb.setItem(row, 0, QTableWidgetItem(f"cell {row}"))
checkbox_item = QTableWidgetItem()
checkbox_item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
checkbox_item.setCheckState(Qt.Unchecked)
self.lb.setItem(row, 1, checkbox_item)
### check every 3rd for testing
for row in range(0, 19, 3):
self.lb.item(row, 1).setCheckState(Qt.Checked)
def get_checked_items(self):
items = []
for row in range(self.lb.rowCount()):
if self.lb.item(row, 1).checkState() == Qt.Checked:
items.append(self.lb.item(row, 0).text())
message = f"checked items: {items}"
print(message)
msg = QMessageBox(1, "checked items", '\n'.join(items), QMessageBox.Ok)
msg.exec()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = mainWin()
win.setWindowTitle("Example")
win.show()
sys.exit(app.exec_())
Posts: 11
Threads: 3
Joined: Apr 2025
|