QAbstractItemModel自动设置检查状态

时间:2015-11-07 05:18:07

标签: qt pyqt pyqt4

我正在尝试自动检查QTreeView中的节点(例如,当用户加载某些数据时)。手动复选框滴答功能正常。我按照http://rowinggolfer.blogspot.com.au/2010/05/qtreeview-and-qabractitemmodel-example.html搜索树中的相关项目,即:

在模型中:

x.diff().dropna()

然后我将相关节点传递给模型以设置其检查状态,例如

def searchModel(self, person):
    def searchNode(node):
        for child in node.childItems:
            if person == child.person:
                index = self.createIndex(child.row(), 0, child)
                return index

            if child.childCount() > 0:
                result = searchNode(child)
                if result:
                    return result

    node_index = searchNode(self.parents[0])
    return node_index

def find_GivenName(self, fname):
    app = None
    for person in self.people:
        if person.fname == fname:
            app = person
            break
    if app != None:
        index = self.searchModel(app)
        return (True, index)            
    return (False, None)

在模型中:

model.setData(node_index, 2, QtCore.Qt.CheckStateRole)

但是没有检查相关节点的复选框。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

正在检查复选框,但仅当鼠标移动到悬停在相关节点上时。根据pyqt文档 - '重新实现setData()函数时,必须明确发出dataChanged()信号' http://pyqt.sourceforge.net/Docs/PyQt4/qabstractitemmodel.html#dataChanged。我将模型中的setData方法更改为:

def setData(self, index, value, role):
    if role == Qt.CheckStateRole:
        row = index.row()
        self.args[row].checked = value  
        self.dataChanged.emit(index, index)
return True

这里有一些关于dataChanged()信号的好信息:When to emit dataChanged from a QAbstractItemModel