1

I am trying to check a node in a QTreeView automatically (eg when user loads some data). Manual checkbox ticking functionality works fine. I search the tree for the relevant item as per http://rowinggolfer.blogspot.com.au/2010/05/qtreeview-and-qabractitemmodel-example.html ie:

In the model:

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)

Then I pass the relevant node into the model to set its check state eg

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

In the model:

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

But the checkbox for the relevant node does not get checked. Any ideas?

Don Smythe
  • 9,234
  • 14
  • 62
  • 105

1 Answers1

0

The checkbox was being checked, but only when the mouse was moved to hover over the relevant node. As per the pyqt docs - 'when reimplementing the setData() function, the dataChanged() signal must be emitted explicitly' http://pyqt.sourceforge.net/Docs/PyQt4/qabstractitemmodel.html#dataChanged. I changed the setData method in the model to:

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

There is some good information of the dataChanged() signal here: When to emit dataChanged from a QAbstractItemModel

Don Smythe
  • 9,234
  • 14
  • 62
  • 105