Results 1 to 5 of 5

Thread: How to create a QList of QPointers?

  1. #1
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    34
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question How to create a QList of QPointers?

    Hi

    I'm trying to create a QList of QPointers, something like this:

    Qt Code:
    1. QList<QPointer<someClass>>* myQPointerList_;
    To copy to clipboard, switch view to plain text mode 

    However, this syntax does not seem to be good. I get errors like:
    1>d:\testproj\mainWindow.h(282) : error C2947: expecting '>' to terminate template-argument-list, found '<'
    1>d:\testproj\mainWindow.h(282) : error C2143: syntax error : missing ';' before '>>'
    1>d:\testproj\mainWindow.h(282) : error C2238: unexpected token(s) preceding ';'
    I use the (Win32) MSVC 2005 compiler.

    So, is there a way to handle this (that is, to create the QList of QPointers that I would like to use)?

  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: How to create a QList of QPointers?

    put a space between the brackets:
    Qt Code:
    1. QList<QPointer<someClass> > myQPointerList_;
    To copy to clipboard, switch view to plain text mode 
    and allocate it on the stack, not on the heap.

  3. #3
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    34
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: How to create a QList of QPointers?

    Well, I'm quite a beginner in C++/Qt, so I'm not sure I fully understand what that means - to "allocate it on the stack, not on the heap" . But, one thing is that I need this QList of QPointers to be accessible/recognized by several methods/"functions" in my main class, which is why I thought I needed to declare it in the header file.

    If I just do
    Qt Code:
    1. QList<QPointer<someQClass> > myQPointerList
    To copy to clipboard, switch view to plain text mode 
    in the constructor of my main class, then myQPointerList will not be "known" by the other methods/"functions" in my main class - right? Or am I wrong - maybe it can be?

  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: How to create a QList of QPointers?

    Quote Originally Posted by dobedidoo View Post
    Well, I'm quite a beginner in C++/Qt,
    ...then please use the Newbie section. We also read and answer thread there and know that the users aren't so good with C++/Qt. Then we answer in more detail.
    so I'm not sure I fully understand what that means - to "allocate it on the stack, not on the heap".
    I wrote that because you had an asterisk in your definition. So for you everything is fine if you use it like:
    Qt Code:
    1. // header file
    2. class MyClass
    3. {
    4. //...
    5. private:
    6. QList<QPointer<someQClass> > myQPointerList;
    7. }
    8.  
    9. // and in the implementation you can use it everywhere
    10.  
    11. void MyClass::someFunction()
    12. {
    13. myQPointerList.append(new someQClass);
    14. // etc.
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to create a QList of QPointers?

    this not related to QList ... this is simple C++ concept... if your declare any variable in constructor then it is not possible to access this outside..

    so make it a member of the class..

    "allocate it on the stack, not on the heap" means
    int a; <-- stack, a will die after its scope, that may be function or object
    int * a = new int; <--heap // a will remain in memory until delete is called

    EDIT: I m TOO late.
    Last edited by nish; 27th November 2009 at 14:19. Reason: Too Late

Similar Threads

  1. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 00:06
  2. Regarding the implementation of QList
    By rachit.gupta in forum Qt Tools
    Replies: 1
    Last Post: 23rd September 2009, 12:41
  3. Eclipse debugging: Unable to create variable object
    By PUK_999 in forum Installation and Deployment
    Replies: 0
    Last Post: 20th August 2009, 21:42
  4. QList inside a QList
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 1st July 2009, 11:59
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

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.