[PyQt] QDirModel instance
Scott Frankel
frankel at circlesfx.com
Fri Jul 11 19:10:37 BST 2008
How does one implement setData() to redraw state changes in a
QDirModel instance?
I've discovered that Qt's dataChanged() member of QDirModel does not
seem to be implemented in PyQt, as per a dir() run over my QDirModel
sub-class:
[ ... ]
customEvent
data
decodeData
[ ... ]
My simple app draws checkboxes next to directory items in its tree,
but clicking the checkbox does nothing, it does not cause the check to
disappear. (Though the state is changed, as per
QtCore.Qt.CheckState().)
I believe the problem is in my implementation of setData, where I emit
a signal to dataChanged(). But if that method doesn't exist ... ?
What's a better strategy to use to force redraws when the underlying
model changes?
Thanks again! And apologies for beating this topic into the ground.
I'm still getting used to PyQt/Qt.
Scott
My setData() method so far:
def setData(self, index, value, role=QtCore.Qt.EditRole):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checkstates[self.fileInfo(index).absoluteFilePath()] =
QtCore.Qt.CheckState()
self
.emit(QtCore.SIGNAL("dataChanged(QtCore.QModelIndex,QModelIndex)"),
index, index)
return True
return QtGui.QDirModel.setData(self, index, value, role)
On Jul 10, 2008, at 2:39 PM, Scott Frankel wrote:
>
> Fabulous!
>
>
> On Jul 10, 2008, at 2:08 PM, Andreas Pakulat wrote:
>
>> On 10.07.08 11:46:36, Scott Frankel wrote:
>>>
>>> Right. Thanks for pointing out that I still need to explicitly pass
>>> "self."
>>>
>>> I'm still not getting checkboxes displayed in the tree widget,
>>> though. I
>>> believe the problem is in the nested return value in the model's
>>> data()
>>> method.
>>>
>>> The C++ example code I'm following uses a QMap data structure to
>>> hold
>>> checkState values. After reading Assistant notes, a python
>>> dir(QtCore),
>>> and some googling, I've found that QMap is not implemented in
>>> PyQt. So
>>> I'm using a Dict instead. Still, my app complains:
>>>
>>> TypeError: invalid result type from DirModel.data()
>>>
>>>
>>> Here's the C++ code I'm following:
>>>
>>> virtual QVariant data(const QModelIndex &index, int role =
>>> Qt::DisplayRole) const
>>> {
>>> if (role == Qt::CheckStateRole && index.column() == 0) {
>>> return
>>> checkstates.value(fileInfo(index).absoluteFilePath(),
>>> Qt::Unchecked);
>>
>> You need to wrap that in a QVariant, i.e.
>>
>> return QVariant(....)
>>
>> data() needs to return a QVariant and Python doesn't support the
>> implicit constructors as C++ does.
>>
>> Andreas
>>
>> --
>> You will pass away very quickly.
>> _______________________________________________
>> PyQt mailing list PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
> Scott Frankel
> President/VFX Supervisor
> Circle-S Studios
> 510-339-7477 (o)
> 510-332-2990 (c)
>
>
>
>
> _______________________________________________
> PyQt mailing list PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
More information about the PyQt
mailing list