Results 1 to 9 of 9

Thread: Problem using QPointer

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Problem using QPointer

    When I use this statement:

    QPointer<QStringList> sl = new QStringList();

    I get this error:

    cannot convert 'QStringList' to 'QObject' in initialization

    But when I try replacing QStringList with QLabel it seems to work OK.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem using QPointer

    QPointer can only be used with classes that are a subclass of QObject. QStringList is not!

    And by the way: It's a bad idea to create a string list on the heap. There is no need since the string list is implicitly shared.

  3. #3
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem using QPointer

    What do you mean by "implicitly shared"? I don't understand why it is a bad idea to create it on the heap?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem using QPointer

    Quote Originally Posted by weaver4 View Post
    What do you mean by "implicitly shared"?
    See http://doc.trolltech.com/4.6/implicit-sharing.html
    I don't understand why it is a bad idea to create it on the heap?
    An int or double you normaly create on the stack, not the heap. So trat QStringList like a basic type. "bad" because it is faster to create it on the stack and and you don't have to take care about deleting etc.

  5. #5
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem using QPointer

    I am kinda new to QT, I did not know about the "implicitly shared" objects; pretty cool.

    I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile. Since this is a subclass would it be "implicitly shared" too?

    Thanks for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem using QPointer

    Quote Originally Posted by weaver4 View Post
    I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile.
    Subclassing QStringList is pretty "dangerous". If you only need that two methods (that don't chance anything at the string list) it is better to define (global) functions to do that.

  7. #7
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem using QPointer

    Why is it "Dangerous"? (I said I was new to QT, trying to figure this Framework out.)

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem using QPointer

    You won't blew your computer up, but things can easily go wrong (performence loose, leaks) if you are not 100% sure of what you are doing. This is the case for such basic Qt storing containers. And would you subclass int or double to ad a save function? Treat QStringList, QString, QPixmap etc. as such generic typs.
    And to your question if your subclass looses implicit sharing: I don't know for sure. It seems that it don't. But I am not sooo deep into the Qt sources that I can say it for sure.

  9. #9
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem using QPointer

    Quote Originally Posted by weaver4 View Post
    I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile. Since this is a subclass would it be "implicitly shared" too?
    Remember that “implicit data sharing” is nothing magical: it just means the class contains a handle to reference-counted data that’s shared as long as the class is certain that the data won’t change. Typical implementations of std::string work the same way.

    We commonly speak of “implicitly shared” classes, but more precisely, it’s data that is implicitly shared. In the case of QStringList, the QString handles and organizational framework that make up the QList of QStrings are shared. Deriving from the QStringList won’t change that; in fact, the data in QStringList is implicitly shared because it is derived from QList<QString>, and QLists are implicitly shared.

    Of course, that won’t make any additional data your class contains or manages implicitly shared.

Similar Threads

  1. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 20:39
  2. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39
  3. QPointer and thread safety
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2008, 09:22
  4. MetaPointer: QPointer with Signals and Slots
    By ntessore in forum Qt-based Software
    Replies: 2
    Last Post: 21st May 2008, 20:17
  5. QPointer and double deletion
    By mtrpoland in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:49

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.