PDA

View Full Version : I need idea for code desing



The Storm
9th August 2008, 13:46
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. :)

jacek
9th August 2008, 15:48
Use signals and slots mechanism, but first design interfaces of all window classes.

The Storm
10th August 2008, 12:40
How I can use signals and slot when I don't have the pointer of the other window to connect it?

jacek
10th August 2008, 22:01
If the object exist, you can always get a pointer to it.

netuno
10th August 2008, 22:13
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

The Storm
11th August 2008, 00:57
Thanks, I will try my best and I will post if I have problems. :)


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

jacek
11th August 2008, 01:18
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.