PDA

View Full Version : Get custom data from qstandarditem?



alexandernst
10th August 2009, 03:42
I'm saving a custom type of data (in fact instance of a custom data) in a QStandardItem this way:

item.setData(QVariant(my_data))

Now I'm trying to get the data back this way:

data = item.data()

But print type(data) says that data is QVariant.

How can I get my data back? Thanks

numbat
10th August 2009, 06:32
This is probably Python specific. In C++, you can do:


MyType variable = variant.value<MyType>();

// or use one of the built in to* functions.
QString str = variant.toString();

alexandernst
10th August 2009, 19:14
In python you don't have to worry about types of data,

a = 1234 will make a to be an int, and
b = "abcd" will make b to be a string.

So, the first part of your example would be:

variable = ..........

The second part is a little bit more confusing :S. QVariant doesn't have any "value" method. Maybe I understood you wrong?

alexandernst
11th August 2009, 04:24
Once again, I made it by myself =)

First, I was storing the data in a bad way. The default QtRole in setData method is QtEditRole, and I needed VisibleRole. And second, once having the QVariant object, I needed to call the toPyObject() method, and I got the instance ^^

Thanks anyway for reading/trying to help. Really, qtcentre rocks!