Results 1 to 2 of 2

Thread: QVariant with QString / who free's its data?

  1. #1
    Join Date
    Oct 2010
    Qt products
    MacOS X Unix/X11 Windows

    Default QVariant with QString / who free's its data?


    Suppose I have the following:

    QAction *a = ....

    a->setData(QString("Hello world"))
    QObject::connect(a, ....)

    Later in my slot:

    QAction *a = QObject::sender();

    QString s = a->data()->toString()

    This works fine. But I wonder who takes care of disposing the associated action data (in this case the QString)

  2. #2
    Join Date
    Jan 2006
    Thanked 1,005 Times in 913 Posts
    Qt products
    Unix/X11 Windows Symbian S60
    Wiki edits

    Default Re: QVariant with QString / who free's its data?

    Qt uses implicit sharing in some classes. So it does for QString:
    Qt Code:
    1. Foo::Foo()
    2. {
    3. QAction *a = QAction(this);
    4. a->setData(QString("Hello world"));
    5. }
    7. Foo::~Foo()
    8. {
    9. // a gets destroyed and so also the string since a takes care of that. (And it the reference count is equal 0.
    10. }
    12. Foo::mySlot()
    13. {
    14. QString s = a->data()->toString(); // s is not a real copy (as long as you don't change it
    15. } // s gets deleted (or the reference count gets decreased.
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 25th August 2010, 09:56
  2. Using QVariant with custom data type
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2009, 13:04
  3. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 13:14
  4. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 16:36
  5. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 15:55

Tags for this Thread


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.