[PyQt] QDirModel instance
Scott Frankel
frankel at circlesfx.com
Thu Jul 10 19:46:36 BST 2008
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);
}
return QDirModel::data(index, role);
}
private:
QMap<QString, Qt::CheckState> checkstates;
};
Here's my code:
class DirModel(QtGui.QDirModel):
def __init__(self, parent=None):
QtGui.QDirModel.__init__(self, parent)
self.checkstates = {}
def data(self, index, role=QtCore.Qt.DisplayRole):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checkstates[self.fileInfo(index).absoluteFilePath()] =
QtCore.Qt.Checked
return self.checkstates[self.fileInfo(index).absoluteFilePath()]
# <-- problem???
return QtGui.QDirModel.data(self, index, role)
Thanks in advance for your suggestions!
Scott
On Jul 9, 2008, at 11:36 PM, Phil Thompson wrote:
> On Wed, 9 Jul 2008 22:39:27 -0700, Scott Frankel <frankel at circlesfx.com
> >
> wrote:
>>
>> Hello,
>>
>> I just joined this list and am new to PyQt (and Qt). I've scanned
>> the
>> archives, but I'm not sure even what I'm looking for to answer my
>> question.
>>
>> I'm looking over some C++ code, trying to rewrite it in Python. (My
>> Spanish is much better than my C++!) I'm sub-classing and extending
>> QDirModel and getting the following error:
>>
>> TypeError: first argument of unbound method QDirModel.data() must be
>> a QDirModel instance
>>
>> The C++ methods are:
>>
>> 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);
>> }
>> return QDirModel::data(index, role);
>> }
>>
>>
>> My translation so far looks like this:
>>
>>
>> [ ... ]
>> class DirModel(QtGui.QDirModel):
>> def data(self, index, role=QtCore.Qt.DisplayRole):
>> if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
>> return checkstates.value(fileInfo(index).absoluteFilePath(),
>> QtCore.Qt.Unchecked)
>> return QtGui.QDirModel.data(index, role)
>> [ ... ]
>>
>>
>> Not sure how to make the first argument (index or self?) be a
>> QDirModel instance. Ultimately I'm hoping for a directory tree with
>> check boxes.
>>
>> Suggestions?
>
> When using an unbound method (including __init__()) you must
> explicitly
> pass self. So...
>
> return QtGui.QDirModel.data(self, index, role)
>
> Phil
>
>
Scott Frankel
President/VFX Supervisor
Circle-S Studios
510-339-7477 (o)
510-332-2990 (c)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080710/db7f5416/attachment.html
More information about the PyQt
mailing list