Results 1 to 5 of 5

Thread: Saving object pointer

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Saving object pointer

    Hi to all!

    I would kindly need some help. I have, for instance, two classes:

    Class A:
    Qt Code:
    1. include "classb.h"
    2. class A
    3. {
    4. public:
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    and class b:
    Qt Code:
    1. include "classa.h"
    2. class B
    3. {
    4. QPointer<class a> m_pPointerToA;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The compiler does not compile this code and here is my question:

    In the constructor of object A I create new object from class B. But, class B must remember it's caller pointer. Parent is remembered only in constructor. How do I save caller pointer in child constructor??
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving object pointer

    QPointer only works on QObject. On the other hand, you talk about parents and children which suggests you are using QObjects. You can access the parent outside constructor via QObject::parent().
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Saving object pointer

    Ok, but I cannot acces custom methods of class via parent() call. Why??
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving object pointer

    Because parent() returns a pointer to QObject. QObject does not offer those custom methods. You could cast the pointer to appropriate type, but I would recommend using signals and slots instead.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Saving object pointer

    Ok, jpn, I see this problem of mine is more design than coding question. Let me show you big picture: I have class CMerchandizeWindow, which interacts with database and shows merchandize pictures in wdiget. Then I have COperationWindow, which is this "parent" of CMerchandizeWindow, because CMerchandizeWindow widget (inhertihs QWidget) is created in COpeation WIndow. In COperationWindow I also have QTextEdit and bunch of buttons. Now, how do I transfer collected data from database (only CMerchandizeWindows interacts with database) from CMerchandizeWindow to QTextEdit (which is declared in COperationWindow)?
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Pointer to ActiveX object in QT
    By json84 in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2007, 12:42
  2. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19
  3. Signals destroys my pointer
    By Jojo in forum Qt Programming
    Replies: 12
    Last Post: 7th March 2006, 21:05

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.