View Full Version : Question regarding QT ui

5th June 2010, 17:48
Hello I am making a program for work using Mainwindow... from that window I have several QDialogs.. one of the dialogs signal/slots is suppose to add items to a QTreeWidget that is located on ui Mainwindow... however i cannot get to access the QTreewidget from the dialog.cpp so I cannot modify anything... I ve tried inheritance and different things but it doesn't seem to work... any ideas on how to approach this??

thank you!!!!!!!

5th June 2010, 17:56
One solution is to work with signals and slots.

Create a slot slotNewItemsToAdd(list of items) in your mainwindow
And create a signal newItemsToAdd(list of items) in your dialog.
Connect them and that should be it.

You do need to add your custom types to the moc though.
If you need help with that, ask

5th June 2010, 18:36
Although there aren't any hard and fast rules about such things, I normally discourage cross-talk between GUI components as much as possible, In a case like this, I would store the dialog's settings in the dialog itself, then extract them if the user accepts the dialog, ignoring them if the user rejects it. Having various GUI objects changing others can lead to a logic and maintenance snarl that's difficult to recover from. It's best to let a central widget (like the main application) handle all such changes, and pass them along to the appropriate components.

There are obviously times when direct manipulation makes sense. But they're normally rare, and when I find myself beginning to allow such communication, I take it as a signal that I'm doing something wrong, and need to rethink my approach to the problem.

5th June 2010, 19:02
I completely agree with SixDegrees. Objects should be as encapsulated as is practical. There is very rarely a need for one form to access another forms UI elements, which is why they are private by default.