PDA

View Full Version : getting data & QModelIndex



alexandernst
5th August 2009, 23:29
[PYTHON]

I have a def


def sizeHint(self, option, index)

witch receives 2 args, QStyleOptionViewItem and QModelIndex. (its a reimplementation of sizeHint, for drawing items in a qtreeview).
As you can guess, I can't access the model where I have all the data because the def is located in another class. So, the question is, ¿how can I get the data of certain column, at the row from the "index" value, having the qmodelindex?

I have been looking at the docs, and I can do a index.model() witch will return me a QAbstractItemModel, but I got stuck there.

What should I do? Should I access the model from the other class? If there is a way of doing what I want to do without having to access the model from the another class it will be better.

Thanks

numbat
6th August 2009, 05:35
You can use the model->index function in a call to data. Something like:


const QAbstractItemModel * model = index.model();
QVariant vt = model->data(model->index(index.row(), column, index.parent()), Qt::DisplayRole);

alexandernst
6th August 2009, 11:38
That works great! =D Thanks!

However, I have some little problem.
I have a QStandardItem with a boolean value. (in column 4)



model = index.model()
vt = model.data(model.index(index.row(), 4, index.parent()), Qt.DisplayRole)
if vt.toBool() == False:
print "Nothing here"
else:
print "Hey! I found something!"


I guess that .toBool tries to convert a QStandardItem to a boolean value instead of convert the value contained in the qstandarditem, and that's why it always return false when I'm sure that there are "true" items.

The .convert method from QVariant doesn't seem to be able to convert to qstandarditem, so, how could I do it? How could I convert the QVariant to QStandardItem?

alexandernst
6th August 2009, 20:43
Oh, I made it :). It was the role... I forgot to chage it to "DecorationRole".

Thanks for the help!