当QListview项目的复选框发生变化时发出信号

时间:2018-03-05 15:34:15

标签: python pyside

如果更改了qlistview项目的复选框,我该如何发出信号?理想情况下,发出的信号会有一些指向更改项目的指针,因此我可以在PySide中相应地执行操作。

enter image description here

SELECT *  FROM [Tfs_Warehouse].[dbo].[DimWorkItem]

1 个答案:

答案 0 :(得分:1)

在Qt4中,没有信号表明如果QCheckBox被选中QAbstractItemView,则仅在Qt5中修改dataChanged信号以发送已修改的角色,从而区分是否检查是否已更改。

在Qt4中,有几种方法可以创建该信号,一种方法是使用委托来跟踪该角色的变化,如下所示:

class StyledItemDelegate(QtGui.QStyledItemDelegate):
    checked = QtCore.Signal(QtCore.QModelIndex, int)
    def editorEvent(self, event, model, option, index):
        if model.flags(index) & QtCore.Qt.ItemIsUserCheckable:
            # before the change
            last_value = index.data(QtCore.Qt.CheckStateRole)
        value = QtGui.QStyledItemDelegate.editorEvent(self, event, model, option, index)
        if model.flags(index) & QtCore.Qt.ItemIsUserCheckable:
            # after the change
            new_value = index.data(QtCore.Qt.CheckStateRole)
            if last_value != new_value:
                self.checked.emit(index, new_value)
        return value

class MegaMergeWindow(QtGui.QMainWindow):
    def __init__(self, *args, **kwargs):
        [...]

        # controls
        self.ui_files = QtGui.QListView()
        self.ui_files.setModel(QtGui.QStandardItemModel())
        self.ui_files.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        delegate = StyledItemDelegate()
        delegate.checked.connect(self.on_checked)
        self.ui_files.setItemDelegate(delegate)
        [...]

    def on_checked(self, index, state):
        text = "Checked" if state == QtCore.Qt.Checked else "UnChecked"
        item = self.ui_files.model().itemFromIndex(index)
        print(item, item.data())
        print(index, index.data())
        print(state)
        print(text)