I need idea for code desing
Hi all,
I'm making Qt GUI application with about 5 windows and I need idea how to desing the code. Lets say I have two forms, the first is the main application window the second is some other window. Now here's the deal - I will initalise the main window from the main() function as an object not pointer, after that in the MainWindow class I will put pointer from the other(secondary) window. I will be able to change the UI in to the secondary window from the main window but I will not be able to change things on the main window via the secondary window because he will not have a pointer to the main window. So I ask how I can update both windows UI's from each other classes without to use global pointers.
Thanks. :)
Re: I need idea for code desing
Use signals and slots mechanism, but first design interfaces of all window classes.
Re: I need idea for code desing
How I can use signals and slot when I don't have the pointer of the other window to connect it?
Re: I need idea for code desing
If the object exist, you can always get a pointer to it.
Re: I need idea for code desing
From what I understand your code is something like this right:
/* inside main() */
QMainWindow window1;
QMainWindow *window2 = new QMainWindow();
Is that right?
There are a few ways to deal with this:
1. Pass the second window pointer to the main window, something like this:
window1.setWindow2(window2);
then on the window1 class (subclass from QMainWindow) just do with window2 what you want.
2. Use signals ands slots. Like this:
/* on main() */
connect(&window1, SIGNAL(backgroundChanged(QColor)), window2, SLOT(changeBackground(QColor)));
3. Make window2 a singleton. That way you only have an instance of it, which you can use as you like.
You can also use tcp/ip to comunicate between both of than but it doesn't seem like a good solution for this case.
I hope this helps
Re: I need idea for code desing
Thanks, I will try my best and I will post if I have problems. :)
Quote:
Originally Posted by
jacek
If the object exist, you can always get a pointer to it.
How exactly, if I'm not able to see the object in the code that I need to get it's pointer? :)
Re: I need idea for code desing
Quote:
Originally Posted by
The Storm
How exactly, if I'm not able to see the object in the code that I need to get it's pointer? :)
You can pass it there from the point in which you created the object. If you have a window that does something useful, then there must exists some interface between that window and the rest of your code. That's the place where you use signals and slots.