Results 1 to 4 of 4

Thread: convert QVariant to QAction

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default convert QVariant to QAction

    I've set a pointer to a QAction as a QWidget property as following:
    Qt Code:
    1. myWidget->setProperty("action", actionPtr);
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: convert QVariant to QAction

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    sepehr (5th April 2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert QVariant to QAction

    Quote Originally Posted by wysota View Post
    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?
    ---
    A situtation never gets so complicated that it can't get any more complicated!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: convert QVariant to QAction

    Qt Code:
    1. QAction *act = ...
    2. QVariant var = (int)act;
    3. //...
    4. QAction *act2 = (QAction*)var.toInt();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  2. Having some QObject problems
    By Barvik in forum Qt Programming
    Replies: 7
    Last Post: 13th November 2008, 04:29
  3. Getting Microsoft Word Object to SaveAs
    By jvwebb in forum Newbie
    Replies: 3
    Last Post: 2nd September 2008, 19:27
  4. Replies: 8
    Last Post: 16th July 2008, 14:05
  5. How to convert QVariant to my custom object?
    By troorl_ua in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 18:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.