Results 1 to 13 of 13

Thread: QList & QPointer to store object list

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

    Exclamation QList & QPointer to store object list

    Hello, there. it's me again

    I have a class HTMLWindow
    Qt Code:
    1. class HTMLWindow : public QWidget
    2. {
    3. Q_OBJECT
    4. ......
    5. }
    To copy to clipboard, switch view to plain text mode 


    and I have a class as QApplication that manage thoses windows
    Qt Code:
    1. class HTMLWindow;
    2.  
    3. class WidgetManager : public QApplication
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. WindowManager(int &argc, char **argv);
    9. ~WindowManager();
    10.  
    11. void addWindow(void);
    12.  
    13. QList<QPointer<HTMLWindow> > WindowList;
    14.  
    15. private:
    16. QSystemTrayIcon *trayIcon;
    17. QMenu *trayIconMenu;
    18. QAction *quitAction;
    19. };
    To copy to clipboard, switch view to plain text mode 

    then my problem is in WindowManager::addWindow()
    Qt Code:
    1. void WidgetManager::addWindow(void){
    2. HTMLWindow *newwindow = new HTMLWindow();
    3. newwindow.setGeometry(QRect(100,100,285,110)); // error1
    4. WindowList->append(newwindow); //error 2
    5. }
    To copy to clipboard, switch view to plain text mode 

    Error 1 is : request for member `load' in `newwindow', which is of non-class type `HTMLWindow*'
    Error 2 is : base operand of `->' has non-pointer type `QList<QPointer<HTMLWindow> >'


    I followed the example browser in Qt\demos\browser

    when i call addWindow the newWindow is created but seems i cannot access its method


    I dont find where is the problem, are the two error linked ?

    thx for your help

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

    Default Re: QList & QPointer to store object list

    I Managed to solve Error 1 by doing

    Qt Code:
    1. HTMLWindow(newwindow).setGeometry(QRect(100,100,285,110));
    To copy to clipboard, switch view to plain text mode 

    seems newwindow is a pointer not an object

    A new problem is the window is created 3 times then destroyed 3 times (i put msgbox in constructor/destructor)

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

    Unhappy Re: QList & QPointer to store object list

    I also replaced
    Qt Code:
    1. QList<QPointer<HTMLWindow> > WindowList;
    To copy to clipboard, switch view to plain text mode 

    by

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

    and now the application end itself with an error code -1073741819

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList & QPointer to store object list

    do you initialize that pointer
    QList<QPointer<HTMLWindow> > *WindowList;
    in your constructor?

    Calling "->append" on an unitialized pointer is a sure way to have "fun" ;-)
    (fun of course meaning usually a crashing app)

  5. #5
    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 caduel View Post
    do you initialize that pointer

    in your constructor?

    Calling "->append" on an unitialized pointer is a sure way to have "fun" ;-)
    (fun of course meaning usually a crashing app)
    No I didnt. But i dont think
    Qt Code:
    1. QList<QPointer<HTMLWindow> > *WindowList;
    To copy to clipboard, switch view to plain text mode 
    is a correct declaration

    and i read on another post of this forum that QList dont need to be initialized

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList & QPointer to store object list

    this should work
    Qt Code:
    1. ...
    2. QList<QPointer<HTMLWindow> > WindowList;
    3. WindowList.append(new HTMLWindow(this));
    4. WindowList << new HTMLWindow(this);
    5.  
    6. WindowList.at(0)->someMethod();
    7. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. The following user says thank you to spirit for this useful post:

    maddog_fr (8th August 2009)

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

    Default Re: QList & QPointer to store object list

    This nearly work
    Qt Code:
    1. void WindowManager::addHTMLWindow(void){
    2. WindowList.prepend(new HTMLWindow());
    3. HTMLWindow(WindowList.at(0)).setGeometry(QRect(100,100,285,110));
    4. }
    To copy to clipboard, switch view to plain text mode 

    I cannot put "this" in the constructor of HTMLWindow because Qwidget is expected and "this" is a Qapplication

    the HTMLWindow is created and it's method is called but is destroyed at the end of the method ! maybe because i dont give a parent in the constructor ?

  9. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList & QPointer to store object list

    i) QList does not need to be initialized, but QList<...>* is a pointer (to a QList) and all pointer do
    ii) QList<QPointer<HTMLWindow> > *WindowList;
    is definitely correct. Maybe it is not what you want, but it is correct.
    iii)
    WindowList.prepend(new HTMLWindow());
    HTMLWindow(WindowList.at(0)).setGeometry(QRect(100 ,100,285,110));
    creates two HTMLWindows. One with new, one with
    HTMLWindow(WindowList.at(0))
    The second one is a temporary created on the stack and is destroyed once you return from that method.

  10. #9
    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.

  11. #10
    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.

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

    maddog_fr (8th August 2009)

  13. #11
    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.

  14. #12
    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.

  15. #13
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.