How do I check if a checkbox is checked or uncheck

2019-08-02 01:15发布

问题:

I have written the code below:

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_())

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 event or signal but i'm not sure as to how I would set up the signal. I found an example code showing the use of MousePressEvent but i'm not sure as to how I would implement that code using python.

The example is as follows:

#include <QtGui>

class TreeWidget : public QTreeWidget
{
public:
        TreeWidget()
        {
                setColumnCount(1);
                item1 = new QTreeWidgetItem(this);
                item1->setExpanded(true);
                item1->setFlags(item1->flags() | Qt::ItemIsUserCheckable);
                item1->setCheckState(0, Qt::Checked);
                item1->setText(0, "item 1");

                item2 = new QTreeWidgetItem(item1);
                item2->setFlags(item2->flags() | Qt::ItemIsUserCheckable);
                item2->setCheckState(0, Qt::Checked);
                item2->setText(0, "item 2");

                item3 = new QTreeWidgetItem(this);
                item3->setText(0, "item 3");
                item3->setFlags(item3->flags() | Qt::ItemIsUserCheckable);
                item3->setCheckState(0, Qt::Checked);

        }
        void mousePressEvent(QMouseEvent *event)
        {
                QModelIndex indexClicked = indexAt(event->pos());
                if(indexClicked.isValid()) {
                        QRect vrect = visualRect(indexClicked);
                        int itemIndentation = vrect.x() - visualRect(rootIndex()).x();
                        QRect rect = QRect(header()->sectionViewportPosition(0) + itemIndentation
                                , vrect.y(), style()->pixelMetric(QStyle::PM_IndicatorWidth), vrect.height());
                        if(rect.contains(event->pos())) {
                                qDebug() << "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();
}

回答1:

Using itemClicked signal:

class window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(window, self).__init__(parent)
        self.TreeWidget = QtGui.QTreeWidget()
        self.TreeWidget.setColumnCount(1)
        # self.item1/2/3 = ....  save reference to the items
        #   to access them in the callback (check_status)
        item1 = self.item1 = QtGui.QTreeWidgetItem(["Item 1"])
        item1.setCheckState(0, QtCore.Qt.Checked)
        item2 = self.item2 = QtGui.QTreeWidgetItem(["Item 2"])
        item2.setCheckState(0, QtCore.Qt.Unchecked)
        item3 = self.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)

        # connect the itemClicked signal to the callback check_status.
        self.TreeWidget.itemClicked.connect(self.check_status)

    def check_status(self):
        if self.item1.checkState(0) == QtCore.Qt.Checked:
            print('item 1 is checked')
        if self.item2.checkState(0) == QtCore.Qt.Checked:
            print('item 2 is checked')
        if self.item3.checkState(0) == QtCore.Qt.Checked:
            print('item 3 is checked')