Results 1 to 3 of 3

Thread: QSharedPointer in a QVariant and back again

  1. #1
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question QSharedPointer in a QVariant and back again

    I'm trying to store QSharedPointer<MyClass> values in a QVariant (so i can store it as custom data in a QComboBox) using:

    Qt Code:
    1. MyClass* myIns = new MyClass();
    2. QSharedPointer<MyClass> asp(myIns);
    3. QVariant aVariant = QVariant::fromValue(asp);
    To copy to clipboard, switch view to plain text mode 

    how do i retrieve the QSharedPointer<MyClass> back from aVariant ?

    i tried:

    Qt Code:
    1. QSharedPointer<MyClass> retValue = v.value<QSharedPointer<MyClass> >();
    To copy to clipboard, switch view to plain text mode 

    but the compiler doesn't like that.
    note: MyClass is derived of QObject
    Last edited by tomschuring; 24th January 2012 at 05:23. Reason: added inheritance of MyClass

  2. #2
    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: QSharedPointer in a QVariant and back again

    You do it the way you have tried. This works for example:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class MyClass: public QObject {
    5. Q_OBJECT
    6. public:
    7. MyClass(QObject *p = 0): QObject(p) { }
    8. };
    9.  
    10. Q_DECLARE_METATYPE( QSharedPointer<MyClass> )
    11.  
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QCoreApplication app(argc, argv);
    16.  
    17. QSharedPointer<MyClass> in( new MyClass );
    18. QVariant v = QVariant::fromValue( in );
    19.  
    20. QSharedPointer<MyClass> out = v.value<QSharedPointer<MyClass> >();
    21. qDebug() << in << out << (in == out);
    22.  
    23. return 0;
    24. }
    25. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to ChrisW67 for this useful post:

    Snix (2nd December 2013), tomschuring (24th January 2012)

  4. #3
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer in a QVariant and back again

    thanks Chris !!
    that did the job.

    i missed the Q_DECLARE_METATYPE macro.
    just looking in the headers what the macro does.

    not quite sure but it looks like it registers the class with the constructor and destructor, but it seems a bit over my head.
    can easily remember to call the macro whenever i use QVariant for my own class through.


    thanks again,
    tom

Similar Threads

  1. Replies: 4
    Last Post: 4th January 2011, 12:07
  2. emitting QSharedPointer
    By babu198649 in forum Newbie
    Replies: 11
    Last Post: 26th July 2010, 08:51
  3. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 14:22
  4. Storing and reading back template types in QVariant
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2010, 07:40
  5. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48

Tags for this Thread

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.