PDA

View Full Version : Referencing Parent Widget from Child



taylor34
9th March 2006, 19:18
Hello--

I've tried looking up through the database for this, but I couldn't find something exactly similar to what I'm doing. I'm using Qt 3.3 with designer (so I've got a bunch of ui.h files for my dialog boxes). In any case, I've got one parent dialog that opens a child dialog. When that child dialog does some actions, I'd like to change the color of a button on the parent dialog box to change with those actions. Is there any way to reference that parent widget from the child easily? I tried using (from inside the child widget):

parent()->buttonOne->setPaletteForegroundColor(Color)

but it doesn't like the parent() option. Perhaps I'm formatting it wrong? Thanks.

Aaron

Bojan
9th March 2006, 19:58
What is exactly the error message? Or does it crash? If it crashes, make sure you are creating the child dialog with this (the main or parent dialog) as the parent. Also make sure your buttonOne is public. However, this is poor design and brakes laws of encapsulation, abstraction, good OOP style etc... It would be better to maybe make a method setButtonBackgroundColor(QColor &newColor) or something like that, and then use it. I am not sure exactly of your situation and design so I am not sure what would work the best.
Finally, what is Color? that should be a QColor variable, or one of predefined colors, like Qt::red.

Bojan

wysota
9th March 2006, 20:00
My wild guess is that you should cast parent() to the class of your parent widget, for example:


((MainWindow*)parent())->buttonOne->setPaletteForegroundColor(Color);

taylor34
9th March 2006, 20:15
The error is is:

'class QObject' has no member named 'buttonOne'

btw, buttonOne is just the generic name I used for an example. My real code uses a name like taEditButton.

I'd like to do that class cast, but I can't because my child widget class has no knowledge of the parent class (i.e. I'm not including the parent.h file or anything).

Thanks again

Aaron

wysota
9th March 2006, 20:51
Then you can't access parents widgets, if you have no knowledge of them. You should somehow pass a pointer to the parent or move that functionality to the parent (which obviously has the knowledge of itself) for example using signals and slots.

yop
9th March 2006, 21:56
I'd like to do that class cast, but I can't because my child widget class has no knowledge of the parent class (i.e. I'm not including the parent.h file or anything).

When you are starting to put those little arrows one after the other the compiler generates indermediate objects / pointers to objects, so not including the interface of the generated objects the compiler won't know what to do and also you can only have access to the public interface:

parent()->DoSomething(); is actually

QObject * temp = parent();
temp->DoSomething(); // boomwhile you need
#include "myform.h"
//...
MyForm *temp = (MyForm *)parent();
temp->DoSomething()

Lebowski
11th April 2006, 13:39
while you need
#include "myform.h"
//...
MyForm *temp = (MyForm *)parent();
temp->DoSomething()
I tried that but I'm getting segmentation fault which means that I'm getting the wrong pointer from parent().

fullmetalcoder
11th April 2006, 14:18
I tried that but I'm getting segmentation fault which means that I'm getting the wrong pointer from parent().

did you checked the validity of the casted pointer ???
basically, you should test for a NULL pointer each time you need to access such a pointer...



if ( !temp )
qDebug("crap!!!");
else
temp->doSomething()


That would do! If you get the "crap!!!" message, check the validity of your algorithm (I mean, be sure that you're not working with the bad pointer type) or try other ways of casting your pointer (RTTI may help).

wysota
11th April 2006, 15:13
You can use dynamic_cast or qobject_cast here to stand a chance when guessing the parent type.