Results 1 to 13 of 13

Thread: QList & QPointer to store object list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    It's exact.
    Qt Code:
    1. WindowList.prepend(new HTMLWindow());
    To copy to clipboard, switch view to plain text mode 
    create a window that is not destroyed at the end of the method
    but
    Qt Code:
    1. WindowList.at(0).setGeometry(QRect(100,100,285,110));
    To copy to clipboard, switch view to plain text mode 
    generate this error : const class QPointer<HTMLWindow>' has no member named 'setGeometry'

    I guess it's normal because the QList contains pointers to HTMLWindow(s) object
    but how to use the object referenced by pointers ?

    that's why i used HTMLWindow(WindowList.at(n)). it is how i do Typecast in Delphi.
    Last edited by maddog_fr; 8th August 2009 at 09:51.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    You should read some books about C++ basics I think...
    And QPointer behaves in the same way as normal pointer, so getting back to one of the first lessons about C++ and pointers you will see that:
    1. If you want to reference the member of the object you use '.' operator:
    Qt Code:
    1. SomeClass obj;
    2. obj.someMemberFunction();
    To copy to clipboard, switch view to plain text mode 
    2. But if you want to reference the member of the object, but you have only the pointer to that object, you have to use '->' operator:
    Qt Code:
    1. SomeClass *obj = new SomeClass;
    2. obj->someMemberFunction();
    To copy to clipboard, switch view to plain text mode 

    And a QPointer<SomeClass> behaves exactly in same way as SomeClass * so you have to use -> operator to get the members from object pointed by a QPointer.

    But you are trying to do very weird things, which are not even supposed to work:
    Qt Code:
    1. HTMLWindow(WindowList.at(n));
    To copy to clipboard, switch view to plain text mode 
    You are storing QPointer<HTMLWindow> in your list so:
    Qt Code:
    1. WindowList.at(n)
    To copy to clipboard, switch view to plain text mode 
    returns a QPointer that points to some object of HTMLWindow class, and it can be used like normal pointer, so the line:
    Qt Code:
    1. HTMLWindow(WindowList.at(n));
    To copy to clipboard, switch view to plain text mode 
    creates a new HTMLWindow object on a stack (a local variable in other words) with a existing HTMLWindow on index 'n' in your list as a parent because in that case the:
    Qt Code:
    1. WindowList.at(n)
    To copy to clipboard, switch view to plain text mode 
    is casted to QWidget *, if constructor of the HTMLWindow looks like :
    Qt Code:
    1. HTMLWindow(QWidget *parent);
    To copy to clipboard, switch view to plain text mode 
    And what you are trying to do is to cast the pointer to an object to instance of that object which obviously should fail, because how do you think the 32-bit (or more) integer value which the memory address of the object can be casted to that object, the whole HTMLWindow???

    The next thing is that (as I said before) if this:
    Qt Code:
    1. WindowList.at(0);
    To copy to clipboard, switch view to plain text mode 
    returns the first element of your list, which is of type QPointer, doing like this:
    Qt Code:
    1. WindowList.at(0).setGeometry(QRect(100,100,285,110));
    To copy to clipboard, switch view to plain text mode 
    is trying to use the setGeometry() method of a QPointer object, which, as I suppose, does not have the setGeometry() method. If you want to use the setGeometry() of the HTMLWindow oobject pointed by this QPointer you have to use '->' operator:
    Qt Code:
    1. WindowList.at(0)->setGeometry(QRect(100,100,285,110));
    To copy to clipboard, switch view to plain text mode 

    So, first of all, it is C++, not Delphi, so maybe you should find some time to learn some C++ basics before you start coding with it...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. The following user says thank you to faldzip for this useful post:

    maddog_fr (8th August 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    You are right. I found out by myself that using -> instead of . was working
    but i didnt really understand why.

    I will follow your advice and take some information about c++ pointers that seems to be really different from delphi.

    thx for ur help all.

  5. #4
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    Quote Originally Posted by faldżip View Post
    You should read some books about C++ basics I think...

    So, first of all, it is C++, not Delphi, so maybe you should find some time to learn some C++ basics before you start coding with it...
    Just for your information I am learning by example. Just reading a book is too boring.
    Maybe it's a slower method but i am not sure. I learned Delphi without opening a book (just learned a bit turbo pascal at school when i was young). Throwning peoples sentences like "go to read C++ basics first" is not a good way to make people like a language. For me pointers are not basic part of C++. thx anyway.

  6. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    Pointers are used every where in C and C++, and especially in Qt where all your widgets should be made on heap and kept with pointer's - so learning about pointers is necessary. And reading a book can help you understand, why -> is working and . is not working.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Replies: 12
    Last Post: 22nd March 2009, 11:22
  2. Access an object stored in the list.
    By cbarmpar in forum General Programming
    Replies: 2
    Last Post: 21st September 2008, 20:19
  3. Replies: 2
    Last Post: 19th September 2008, 05:21
  4. Possible signal mapper problem
    By MarkoSan in forum Qt Programming
    Replies: 13
    Last Post: 25th January 2008, 13:11

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
  •  
Qt is a trademark of The Qt Company.