On Sun, 2007-12-02 at 12:00 +0100, Sibylle Koczian wrote:
> Hello, Daniel,
> 
> Am Montag, 26. November 2007 13:59:52 schrieb Daniel Lundqvist:
> > Hey,
> >
> > I've been struggling to get the above to work correctly. I've looked
> > around but haven't found a solution to my problem yet.
> >
> > The issue I'm having is I can't get the mapper to update the widgets at
> > all. Not even when I call setCurrentIndex(n).
> >
> > I'm attaching my sample project that shows the problem. The sample
> > contains a simple model with two columns. One combo box show column 0
> > from the model and a QLineEdit should show second column from the index
> > selected in the combo box. (I've also tried connecting
> > QComboBox::activated(int) -> QDataWidgetMapper::setCurrentIndex(int) to
> > no avail).
> >
> 
> 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).

> 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.

> - making column 1 of the model editable

Did you do that by returning ItemIsEditable from flags()?

> What does the QDataWidgetMapper expect from a model?

Nothing more than it being correct :)

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()).

Thanks for looking into this. Attaching a corrected model.

-- 
daniel
from PyQt4 import QtGui, QtCore

words = [
    {'name' : 'day',
     'opposite' : 'night'
     },
    {'name' : 'sunny',
     'opposite' : 'rainy'
     }
    ]

class MyModel(QtCore.QAbstractItemModel):
    def __init__(self, parent = None):
        QtCore.QAbstractItemModel.__init__(self, parent)
        self.cache = {}

    def index(self, row, column, parent = None):
        if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
            return QtCore.QModelIndex()

        return QtCore.QAbstractItemModel.createIndex(self, row, column);

    def parent(self, index):
        return QtCore.QModelIndex()

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

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        if index.isValid() == 0:
            return QtCore.QVariant();

        if index.column() > 1:
            return QtCore.QVariant();

        if index.row() > len(words) - 1:
            return QtCore.QVariant();

        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            if index.column() == 0:
                return QtCore.QVariant(words[index.row()]['name'])
            else:
                return QtCore.QVariant(words[index.row()]['opposite'])
        else:
            return QtCore.QVariant()
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to