How can I iterate one column in QTableWidget
and check if a checkbox is checked?
When I clicked the button in the left, I want to iterate the column in the picture and check if a checkbox is checked.
As of now, I've tried this function but the function only return None
:
def checkBoxcheck(self):
print("checkBoxcheck started")
for row in range(self.rowCount()):
print(self.cellWidget(1, 1))
if not self.cellWidget(1, 1) is None:
if self.cellWidget(row, 1).isChecked():
#others to do
This is the code to use in order to reproduce this problem (I already removed those unnecessary parts of the code):
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTableWidget, \
QTableWidgetItem, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtWidgets
import sys
class TableWidget(QTableWidget):
def __init__(self):
super().__init__(10, 3)
self.verticalHeader().setDefaultSectionSize(10)
self.horizontalHeader().setDefaultSectionSize(200)
for rows in range(self.rowCount()):
item = QtWidgets.QTableWidgetItem()
item.setCheckState(QtCore.Qt.Unchecked)
self.setItem(rows, 1, item)
def checkBoxcheck(self):
print("checkBoxcheck started")
for row in range(self.rowCount()):
print(self.cellWidget(1, 1))
if not self.cellWidget(1, 1) is None:
if self.cellWidget(row, 1).isChecked():
#others to do
pass
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(700, 500)
mainLayout = QHBoxLayout()
table = TableWidget()
mainLayout.addWidget(table)
buttonLayout = QVBoxLayout()
button_new = QPushButton('Check if checked')
button_new.clicked.connect(table.checkBoxcheck)
buttonLayout.addWidget(button_new)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
def app():
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())
app()
I hope you can help me with this problem