python 在单击 QTreeview 项目复选框时发出信号已更改

python emit signal on clicked QTreeview item checkbox changed

如何在树视图项的复选框更改时发出信号?

import sys
from PySide import QtGui, QtCore

class Browser(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Browser, self).__init__(parent)

        self.initUI()

    def initUI(self):
        self.resize(200, 300)
        self.setWindowTitle('Assets')
        self.setModal(True)

        self.results = ""

        self.uiItems = QtGui.QTreeView()
        self.uiItems.setAlternatingRowColors(True)
        self.uiItems.setSortingEnabled(True)
        self.uiItems.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)

        grid = QtGui.QGridLayout()
        grid.setContentsMargins(0, 0, 0, 0)
        grid.addWidget(self.uiItems, 0, 0)
        self.setLayout(grid)

        self.show()
        self.create_model()

    def create_model(self):
        items = [
            'Cookie dough',
            'Hummus',
            'Spaghetti',
            'Dal makhani',
            'Chocolate whipped cream'
        ]

        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(['Name'])

        for item in items:
            model.insertRow(0)

            # Append object
            model.setData(model.index(0, 0), QtCore.Qt.Unchecked, role = QtCore.Qt.CheckStateRole)
            model.setData(model.index(0, 0), item)

            item = model.itemFromIndex(model.index(0,0))
            item.setCheckable(True)


        self.uiItems.setModel(model)


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Browser()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

使用来自 QStandardItemModel 的 itemChanged 信号。

import sys
from PyQt4 import QtGui, QtCore

class Browser(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Browser, self).__init__(parent)

        self.initUI()

    def initUI(self):
        self.resize(200, 300)
        self.setWindowTitle('Assets')
        self.setModal(True)

        self.results = ""

        self.uiItems = QtGui.QTreeView()
        self.uiItems.setAlternatingRowColors(True)
        self.uiItems.setSortingEnabled(True)
        self.uiItems.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)

        grid = QtGui.QGridLayout()
        grid.setContentsMargins(0, 0, 0, 0)
        grid.addWidget(self.uiItems, 0, 0)
        self.setLayout(grid)

        self.show()
        self.create_model()

    def create_model(self):
        items = [
            'Cookie dough',
            'Hummus',
            'Spaghetti',
            'Dal makhani',
            'Chocolate whipped cream'
        ]

        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(['Name'])

        for item in items:
            model.insertRow(0)

            # Append object
            model.setData(model.index(0, 0), QtCore.Qt.Unchecked, role = QtCore.Qt.CheckStateRole)
            model.setData(model.index(0, 0), item)

            item = model.itemFromIndex(model.index(0,0))
            item.setCheckable(True)

        # Added the following line.
        model.itemChanged.connect(self.test)
        self.uiItems.setModel(model)

    # Added the following method.
    def test(self, item):
        if item.text() == "Hummus":
            if item.checkState() == QtCore.Qt.Checked:
                # Hummus is selected
                # do something here
                pass
            else:
                # Hummus is deselected
                # do something here
                pass
        if item.text() == "Spaghetti":
            if item.checkState() == QtCore.Qt.Checked:
                # Spaghetti is selected
                # do something here
                pass
            else:
                # Spaghetti is deselected
                # do something here
                pass


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Browser()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

使用 itemChanged 信号的一个主要问题是它不会告诉您 发生了什么变化。如果它发送已更改数据的特定角色,将会更有用。

事实上,总是存在因更改您不感兴趣的其他类型的数据而产生误报的危险(有 fifteen pre-defined data roles,以及任意数量的用户定义的数据)。

因此,更稳健的解决方案是子class 模型并发出专门包含角色的自定义信号:

        model = StandardItemModel()
        ...

        self.uiItems.setModel(model)
        model.itemDataChanged.connect(self.handleItemDataChanged)

    def handleItemDataChanged(self, item, role):
        if role == QtCore.Qt.CheckStateRole:
            print(item.text(), item.checkState())


class StandardItemModel(QtGui.QStandardItemModel):
    itemDataChanged = QtCore.Signal(object, object)

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        oldvalue = index.data(role)
        result = super(StandardItemModel, self).setData(index, value, role)
        if result and value != oldvalue:
            self.itemDataChanged.emit(self.itemFromIndex(index), role)
        return result