PDA

View Full Version : QVariant - storing QIcons[]



moowy
23rd August 2006, 14:35
Hello

I wonder if it's possible to store a table of QIcons[] in QVariant?? I know that you can store one QIcon, but I don't know if you can a table of QIcons??

jpn
23rd August 2006, 14:45
You could use a QList<QVariant> where each item is another QList<QVariant> where variants contain QIcons. :)

moowy
23rd August 2006, 15:22
Thanks. Now that I see I think I'm gonna realise it with QMap<QString,QVariant> and store it in action's data().

moowy
24th August 2006, 11:36
Can u help me with another problem:
I must extract char* out of QVariant. I set it with
action->setData("text");
action is type QAction.

Tnx

jpn
24th August 2006, 11:41
I must extract char* out of QVariant. I set it with
action->setData("text");
action is type QAction.




QString str = action->data().toString(); // maybe this is sufficient?
char* ptr = qstrdup(qPrintable(str)); // if not, remember to delete

moowy
24th August 2006, 11:43
it must be char* , .toString() is nut sufficient

pherthyl
24th August 2006, 18:55
Have a look at QVariant::setValue.

void QVariant::setValue ( const T & value )
Stores a copy of value. If T is a type that QVariant doesn't support, QMetaType is used to store the value. A compile error will occur if QMetaType doesn't handle the type.
Example:
QVariant v;

v.setValue(5);
int i = v.toInt(); // i is now 5
QString s = v.toString() // s is now "5"

MyCustomStruct c;
v.setValue(c);

...

MyCustomStruct c2 = v.value<MyCustomStruct>();

moowy
25th August 2006, 00:41
I tried using that but it failed.

The code that jpn submitted works just fine for me.
QString str = action->data().toString(); // maybe this is sufficient?
char* ptr = qstrdup(qPrintable(str)); // if not, remember to delete

I'm really pleased with this forum as I see that there are a lot of friendly people who are not just trying to be smart but really come with solutions. Tnx