PDA

View Full Version : Saving object pointer



MarkoSan
11th January 2008, 07:53
Hi to all!

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

Class A:

include "classb.h"
class A
{
public:
....
}

and class b:

include "classa.h"
class B
{
QPointer<class a> m_pPointerToA;
}

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??

jpn
11th January 2008, 09:13
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().

MarkoSan
11th January 2008, 09:54
Ok, but I cannot acces custom methods of class via parent() call. Why??

jpn
11th January 2008, 09:58
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.

MarkoSan
11th January 2008, 11:53
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)?