accessing my main application window widget
Hi,
How can I access the main window widget from a child widget N levels down?
e.g. in main.cpp I have
Code:
MainAppUI w;
w.show();
Then within w I create widgets so eventually I have
w->aa->bb->cc->...>xx->yy->zz->bottomwidget
where I never know exactly how many parents there will be.
How then, in bottomwidget, can I access w? In bottomwidget I declare w as a friend class, but buttomwidget needs to access functions and variables in w. How, without writing a function to explicitly pass the pointer to bottomwidget, can I do this?
Thanks!
Jay
Re: accessing my main application window widget
Well... basicly you can call parent() as long as it returns something other than null. The last non-null result returned will be the main window.
Alternatively you can just call qApp->mainWidget() (just remember to #include <qapplication.h> and to set the main widget in main() ).
Re: accessing my main application window widget
Did you figure it out? trying to do almost the same, and after a couple of hours now and x number of crashes... forum is probably the hope for me...
I'm having trouble doing several parent(). results in crashes... :(
Quote:
Originally Posted by jayw710
Hi,
How can I access the main window widget from a child widget N levels down?
e.g. in main.cpp I have
Code:
MainAppUI w;
w.show();
Then within w I create widgets so eventually I have
w->aa->bb->cc->...>xx->yy->zz->bottomwidget
where I never know exactly how many parents there will be.
How then, in bottomwidget, can I access w? In bottomwidget I declare w as a friend class, but buttomwidget needs to access functions and variables in w. How, without writing a function to explicitly pass the pointer to bottomwidget, can I do this?
Thanks!
Jay
Re: accessing my main application window widget
Doing more than one parent() call is a bad design in most cases. In reality, you should have methods in your parent objects to return you a proper pointer of an object you want. Then you'll be sure the pointer return is valid and of proper type.
Re: accessing my main application window widget
Quote:
Originally Posted by
wysota
Well... basicly you can call parent() as long as it returns something other than null. The last non-null result returned will be the main window.
Alternatively you can just call qApp->mainWidget() (just remember to #include <qapplication.h> and to set the main widget in main() ).
I tried setting the main widget in main() like so:
Code:
#include <QApplication>
#include "mymainwidg.h"
int main(int argc, char** argv)
{
MyMainWidg widg;
app.setMainWidget(&widg);
widg.show();
return app.exec();
}
My compiler whines that setMainWidget is undeclared. What have i missed? Was setMainWidget perhaps not available in version 4.2.2?
Re: accessing my main application window widget
Quote:
Originally Posted by
TheRonin
My compiler whines that setMainWidget is undeclared. What have i missed? Was setMainWidget perhaps not available in version 4.2.2?
It's available only as a part of Qt3 Support module.
Re: accessing my main application window widget
Quote:
Originally Posted by
martonmiklos
Hi everyone!
My problem is seems to be similar as jayw710's so I didn't opened a new topic.
I'm designing an application what have a main window.
This was designed in the QT Designer.
I'm subclassed it with the QDevelop IDE to the mainwindowimpl.cpp , .h
It generated the MainWindowImpl class.
I was designed an other dialog whit the designer, subclassed it in the same way.
This is created the smenu.cpp and the smenu.h header file with the settingsmenu class.
I have inculded the smenu.h into the mainwindow.h.
In the class definition of the MainWindowImpl, I created a pointer to the dialog.
....
Jepp...
I find the soulution for my problem. I share it with you probably it could be useful for somebody in the future.
So if you passed the this pointer in the argument list you can use it with this syntax in your child class:
((MainWindowImpl *)parent)->something
Re: accessing my main application window widget
Quote:
Originally Posted by
martonmiklos
Jepp...
I find the soulution for my problem. I share it with you probably it could be useful for somebody in the future.
So if you passed the this pointer in the argument list you can use it with this syntax in your child class:
((MainWindowImpl *)parent)->something
It's better to make the MakeWindowImpl a singleton in these cases.