Results 1 to 3 of 3

Thread: best way to extend QVariant to support std::string?

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default best way to extend QVariant to support std::string?

    I found out that I need to extend support of std::string in QVariant. Specifically, to allow std::string as an argument in constructor.
    Other then deriving from QVariant, (something like myQVariant : public QVariant) and supplying the signatures for all datatypes that are relevant for me, including std::string - may be there is another way? I am not considering the option of chaging Qt sources.

    Thanks

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: best way to extend QVariant to support std::string?

    1. declare meta type in some header file: Q_DECLARE_METATYPE
    2. just after QApplication is created register new type using: qRegisterMetaType
    3. Now you can use QVaiant with almost any type (requirements are listed in Q_DECLARE_METATYPE documentation).

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: best way to extend QVariant to support std::string?

    QVariant is designed to be extended using the mechanisms MarekR22 outlines above. You can then use the named constructor QVariant::fromValue() or QVariant::setValue()
    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. Q_DECLARE_METATYPE(std::string)
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. qRegisterMetaType<std::string>("std::string");
    11.  
    12. std::string s("Test");
    13. QVariant v1( QVariant::fromValue<std::string>(s) );
    14. v2.setValue(s);
    15.  
    16. std::string t1 = v1.value<std::string>();
    17. std::cout << t1 << std::endl;
    18. std::string t2 = v2.value<std::string>();
    19. std::cout << t2 << std::endl;
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 4th January 2011, 12:07
  2. How to add metatype support for QMap<int,QVariant>?
    By makinenh in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2010, 16:15
  3. Replies: 2
    Last Post: 10th December 2009, 20:51
  4. How to extend a class ?
    By agent007se in forum Newbie
    Replies: 11
    Last Post: 27th June 2006, 12:09
  5. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 09:55

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.