title: How to check if a checkbox is checked in a qtablewidget?
body: <p>I'm setting a filtering tool in Python 3.7 and I'm using pyqt5. I've created a qtablewidget to store the filters and their complementaries chosen by the user. I'd like to allow the user to combine filters over a data so I added grouped checkboxes  in each row to select wanted filters.</p>
  <p>Which commands should I use to loop over my qtable to get which checkbox is selected please?</p>
  <p><img src="https://i.stack.imgur.com/7aUnc.png" alt="enter image description here" /></p>
  <pre class="lang-py prettyprint-override"><code>def bindApply(self):
      checked_list = []
      for i in range(self.tableWidget.rowCount()):
          #print(self.tableWidget.rowCount())
          if self.tableWidget.item(i, 1).checkState() == QtCore.Qt.Checked:
              checked_list.append([i,1])
          elif self.tableWidget.item(i, 2).checkState() == QtCore.Qt.Checked:
              checked_list.append([i,2])
          else:
              pass
      return(checked_list)
  </code></pre>
  <p>I expect a list containing the indexes of selected rows and columns but my function returns nothing.</p>
  <p>Here is a reproducible example:</p>
  <pre class="lang-py prettyprint-override"><code>from PyQt5 import QtCore, QtGui, QtWidgets
  
  class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
          MainWindow.setObjectName(&quot;MainWindow&quot;)
          MainWindow.resize(550, 350)
          MainWindow.setMinimumSize(QtCore.QSize(550, 350))
          MainWindow.setMaximumSize(QtCore.QSize(550, 350))
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName(&quot;centralwidget&quot;)
          self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
          self.tableWidget.setGeometry(QtCore.QRect(20, 20, 500, 200))
          self.tableWidget.setObjectName(&quot;tableWidget&quot;)
          self.tableWidget.setColumnCount(3)
          self.tableWidget.setRowCount(0)
          item = QtWidgets.QTableWidgetItem()
          self.tableWidget.setHorizontalHeaderItem(0, item)
          item = QtWidgets.QTableWidgetItem()
          self.tableWidget.setHorizontalHeaderItem(1, item)
          item = QtWidgets.QTableWidgetItem()
          self.tableWidget.setHorizontalHeaderItem(2, item)
          self.pushButton_Save = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton_Save.setGeometry(QtCore.QRect(120, 250, 100, 50))
          self.pushButton_Save.setMinimumSize(QtCore.QSize(100, 50))
          self.pushButton_Save.setMaximumSize(QtCore.QSize(100, 50))
          self.pushButton_Save.setObjectName(&quot;pushButton_Save&quot;)
          self.pushButton_Apply = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton_Apply.setGeometry(QtCore.QRect(260, 250, 100, 50))
          self.pushButton_Apply.setMinimumSize(QtCore.QSize(100, 50))
          self.pushButton_Apply.setMaximumSize(QtCore.QSize(100, 50))
          self.pushButton_Apply.setObjectName(&quot;pushButton_Apply&quot;)
          MainWindow.setCentralWidget(self.centralwidget)
          self.menubar = QtWidgets.QMenuBar(MainWindow)
          self.menubar.setGeometry(QtCore.QRect(0, 0, 550, 21))
          self.menubar.setObjectName(&quot;menubar&quot;)
          MainWindow.setMenuBar(self.menubar)
          self.statusbar = QtWidgets.QStatusBar(MainWindow)
          self.statusbar.setObjectName(&quot;statusbar&quot;)
          MainWindow.setStatusBar(self.statusbar)
  
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, &quot;MainWindow&quot;))
          item = self.tableWidget.horizontalHeaderItem(0)
          item.setText(_translate(&quot;MainWindow&quot;, &quot;Name&quot;))
          item = self.tableWidget.horizontalHeaderItem(1)
          item.setText(_translate(&quot;MainWindow&quot;, &quot;Filter&quot;))
          item = self.tableWidget.horizontalHeaderItem(2)
          item.setText(_translate(&quot;MainWindow&quot;, &quot;complementary&quot;))
          self.pushButton_Save.setText(_translate(&quot;MainWindow&quot;, &quot;Save&quot;))
          self.pushButton_Apply.setText(_translate(&quot;MainWindow&quot;, &quot;Apply&quot;))
          #============================  BINDING  ===============================
          self.pushButton_Save.clicked.connect(self.bindSave)
          self.pushButton_Apply.clicked.connect(self.bindApply)
          
      def bindSave(self):
          numRows = self.tableWidget.rowCount()
          self.tableWidget.insertRow(numRows)
          
          groupButton = QtWidgets.QButtonGroup(self.tableWidget)
          groupButton.setExclusive(True)
          it1 = QtWidgets.QTableWidgetItem(&quot;filter &quot;+str(numRows))
          self.tableWidget.setItem(numRows, 0, it1)
          ch_bx1 = QtWidgets.QCheckBox()
          groupButton.addButton(ch_bx1)
          self.tableWidget.setCellWidget(numRows, 1, ch_bx1)
          ch_bx2 = QtWidgets.QCheckBox()
          groupButton.addButton(ch_bx2)
          self.tableWidget.setCellWidget(numRows, 2, ch_bx2)
          
      def bindApply(self):
          checked_list = []
          for i in range(self.tableWidget.rowCount()):
              #print(self.tableWidget.rowCount())
              if self.tableWidget.item(i, 1).checkState() == QtCore.Qt.Checked:
                  checked_list.append([i,1])
              elif self.tableWidget.item(i, 2).checkState() == QtCore.Qt.Checked:
                  checked_list.append([i,2])
              else:
                  pass
          print(checked_list)
          
          
  if __name__ == &quot;__main__&quot;:
      import sys
      if not QtWidgets.QApplication.instance():
          app = QtWidgets.QApplication(sys.argv)
      else:
          app = QtWidgets.QApplication.instance()
      MainWindow = QtWidgets.QMainWindow()
      ui        = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
  </code></pre>