[PyQt] QDataWidgetMapper

Sibylle Koczian Sibylle.Koczian at t-online.de
Mon Dec 3 12:19:16 GMT 2007


Am Montag, 3. Dezember 2007 08:45:43 schrieben Sie:
> On Sun, 2007-12-02 at 12:00 +0100, Sibylle Koczian wrote:
> > Did you solve your problem? I've tried several variations, but the only
> > one that did help was putting your data into a QStandardItemModel.
>
> Yes, sorry for not getting back here. The main error was in data(). It
> did not correctly return data when role was EditRole (which is what
> QDataWidgetMapper asks for).
>

Where did you find that? Is it in the documentation, or in one of the 
examples? If so, I've overlooked it. 

> > Things I tried:
> > - subclassing QAbstractTableModel instead of QAbstractItemModel, because
> > your model isn't hierarchical
>
> Yeah, I guess this makes more sense. Some of the functions in my model
> can be eliminated then.
>

And the rowCount() can be simplified again. As it is, it isn't quite correct:

    def rowCount(self, parent):
        if not parent.isValid():
            return len(words)
        else:
            return 0

If parent is None (normal in your model) then it has no attribute isValid(). I 
changed it to "if parent is None or not parent.isValid()", but that's ugly 
and I'm not sure if it's really the logic you meant.

> > - making column 1 of the model editable
>
> Did you do that by returning ItemIsEditable from flags()?
>

Not quite. For this there is an example in the "Model / View programming" part 
of the documentation:

    def flags(self, index):
        flags = QtCore.QAbstractItemModel.flags(self, index)
        if index.column() > 0:
            flags |= QtCore.Qt.ItemIsEditable
        return flags
        
    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if not (index.isValid() and role == QtCore.Qt.EditRole):
            return False
        if index.column() == 1:
            words[index.row()]['opposite'] = value.toString()
        else:
            return False
        self.emit(QtCore.SIGNAL('dataChanged(const QModelIndex &, const 
QModelIndex &)'), index, index)
        return True

> > What does the QDataWidgetMapper expect from a model?
>
> Nothing more than it being correct :)
>

Well, I don't think a model not returning anything for the EditRole is 
necessary wrong in itself and in all circumstances. 

> I used the modeltest.py from contrib to fix some things in my model (but
> it had no way of detecting my error in data()).
>

What and where is that?

Thank you,
Sibylle


More information about the PyQt mailing list