title: How do I check if a checkbox is checked or unchecked? (In QTreeWidget)
body: <p>I have written the code below:</p>
  
  <pre><code>from PyQt4 import QtCore, QtGui
  import sys
  
  class window(QtGui.QMainWindow):
      def __init__(self, parent=None):
          super(window, self).__init__(parent)
          self.TreeWidget = QtGui.QTreeWidget()
          self.TreeWidget.setColumnCount(1)
          item1 = QtGui.QTreeWidgetItem(["Item 1"])
          item1.setCheckState(0, QtCore.Qt.Checked)
          item2 = QtGui.QTreeWidgetItem(["Item 2"])
          item2.setCheckState(0, QtCore.Qt.Unchecked)
          item3 = QtGui.QTreeWidgetItem(["Item 3"])
          item3.setCheckState(0, QtCore.Qt.Unchecked)
          self.TreeWidget.addTopLevelItem(item1)
          self.TreeWidget.addTopLevelItem(item2)
          self.TreeWidget.addTopLevelItem(item3)
          self.setCentralWidget(self.TreeWidget)
  
          if item1.checkState(0) == QtCore.Qt.Checked:
              print('item 1 is checked')
          if item2.checkState(0) == QtCore.Qt.Checked:
              print('item 2 is checked')
          if item3.checkState(0) == QtCore.Qt.Checked:
              print('item 3 is checked')
  
  if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      ui = window()
      ui.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>With the code above, I managed to create checkable boxes. However when I run the program I want to print the item that is checked if a user checks it. To my understanding, I would need to use an <code>event</code> or <code>signal</code> but i'm not sure as to how I would set up the <code>signal</code>. I found an example code showing the use of <code>MousePressEvent</code> but i'm not sure as to how I would implement that code using <code>python</code>.</p>
  
  <p>The example is as follows:</p>
  
  <pre><code>#include &lt;QtGui&gt;
  
  class TreeWidget : public QTreeWidget
  {
  public:
          TreeWidget()
          {
                  setColumnCount(1);
                  item1 = new QTreeWidgetItem(this);
                  item1-&gt;setExpanded(true);
                  item1-&gt;setFlags(item1-&gt;flags() | Qt::ItemIsUserCheckable);
                  item1-&gt;setCheckState(0, Qt::Checked);
                  item1-&gt;setText(0, "item 1");
  
                  item2 = new QTreeWidgetItem(item1);
                  item2-&gt;setFlags(item2-&gt;flags() | Qt::ItemIsUserCheckable);
                  item2-&gt;setCheckState(0, Qt::Checked);
                  item2-&gt;setText(0, "item 2");
  
                  item3 = new QTreeWidgetItem(this);
                  item3-&gt;setText(0, "item 3");
                  item3-&gt;setFlags(item3-&gt;flags() | Qt::ItemIsUserCheckable);
                  item3-&gt;setCheckState(0, Qt::Checked);
  
          }
          void mousePressEvent(QMouseEvent *event)
          {
                  QModelIndex indexClicked = indexAt(event-&gt;pos());
                  if(indexClicked.isValid()) {
                          QRect vrect = visualRect(indexClicked);
                          int itemIndentation = vrect.x() - visualRect(rootIndex()).x();
                          QRect rect = QRect(header()-&gt;sectionViewportPosition(0) + itemIndentation
                                  , vrect.y(), style()-&gt;pixelMetric(QStyle::PM_IndicatorWidth), vrect.height());
                          if(rect.contains(event-&gt;pos())) {
                                  qDebug() &lt;&lt; "checkbox clicked";
                                  QTreeWidget::mousePressEvent(event);
                                  return;
                          } else {
                                  QTreeWidget::mousePressEvent(event);
                          }}}
  private:
          QTreeWidgetItem *item1;
          QTreeWidgetItem *item2;
          QTreeWidgetItem *item3;
          QTreeWidgetItem *item4;
  };
  
  int main(int argc, char **argv)
  {
          QApplication app(argc, argv);
          TreeWidget box;
          box.show();
          return app.exec();
  }
  </code></pre>