PDA

View Full Version : convert QVariant to QAction



sepehr
30th March 2009, 20:42
I've set a pointer to a QAction as a QWidget property as following:


myWidget->setProperty("action", actionPtr);

I send the QWidget to a function and I want to convert the property "action" to a QAction again so I can call one of it's methods, is that possible? if yes please tell me how

wysota
30th March 2009, 22:33
Yes, but you should avoid such weird mechanisms. You can store the pointer as an integer and when you want it back, convert the variant to integer and cast it to the appropriate pointer type.

But I'd like to suggest an alternative. Implement a singleton class (or a global object) that will act as an action manager. First you register actions there and then you can retrieve them back by calling them by name. Then you can store the action's name in QVariant and then ask the manager for a proper action. The simplest action manager is a global QMap<QString,QAction*> object but it's worth implementing something a bit more complex.

sepehr
5th April 2009, 10:27
Yes, but you should avoid such weird mechanisms. You can store the pointer as an integer and when you want it back, convert the variant to integer and cast it to the appropriate pointer type.

would you give an example for this?

wysota
5th April 2009, 11:03
QAction *act = ...
QVariant var = (int)act;
//...
QAction *act2 = (QAction*)var.toInt();