Python Forum

Full Version: QTableWidget - Retrieve content from a cell
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Do you want the text?

items.append(self.tblparc.itemAt(i,0).text())
Yes...I want the text. Tried here and don't understand why the reply repeats the first one that he find.
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']
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...
Use QTableWidgetItem for text and QCheckBox as CellWidget
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_())
All right...thanks.