PDA

View Full Version : accessing my main application window widget



jayw710
20th April 2006, 23:49
Hi,

How can I access the main window widget from a child widget N levels down?

e.g. in main.cpp I have

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

wysota
21st April 2006, 00:42
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() ).

luf
23rd May 2006, 02:00
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... :(


Hi,

How can I access the main window widget from a child widget N levels down?

e.g. in main.cpp I have

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

wysota
23rd May 2006, 03:04
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.

martonmiklos
29th October 2007, 12:40
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.



class MainWindowImpl : public QMainWindow, public Ui::FoAblak
{

Q_OBJECT
public:
MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
~MainWindowImpl();

Ui::FoAblak ui;

smenu *settingsMenu;
int volume;




In the constructor of the MainWindowImpl I create an object from it.




MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
_mixerfd = open("/dev/mixer", O_RDWR);
ui.setupUi(this);
smenu = new settingsMenu(this);

...
void doSomething()
{
qWarning() << "it work's";
};



When I press a button in the main window I call the smenu->show()
The dialog appears, and everything is fine.

But -- and there is my big problem -- I would like to acces from this dialog the MainWindow 's functions, variables, etc.
I know that I have to do something with the constructor's first argument the parent pointer.
For example I can access the mainwindow's width with the parent->width(), but I can not do it with the parent->doSomething().
I would like to ask it from you how can I do it?

TheRonin
29th October 2007, 14:39
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:



#include <QApplication>
#include "mymainwidg.h"
int main(int argc, char** argv)
{
QApplication app(argc, 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?

jacek
31st October 2007, 22:23
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.

martonmiklos
15th November 2007, 19:26
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

marcel
15th November 2007, 20:33
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.