Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to have one logic object with multiple custom widgets for representation?
AlexanderL last edited by
I want to write an application where I have one separate object handling the logic to which I can connect 1 to n independent custom widgets. These custom widgets should be able to send user input to and represent data from the logic object. I want to do this without having to change the implementation of the logic object itself, such that there is a clean separation between gui and logic.
For better understanding, a simplified/random example of what i want:
Let's say i have a logic object (and there must be only one object of this class) which has three strings A, B, C and updates them via network I/O. Furthermore i want to have a custom widget X which displays strings A and B an has a button to the tell the object to update the strings and a custom widget Y which only displays strings A and C. The custom widgets should update their displayed information if there's a change in the logic object.
I'm currently using Visual Studio 2010, Qt Designer and the "member variable approach ":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#using-a-member-variable to create the custom widgets. I put them together in the main window ui file with the "promote to" command.
So far, I have the logic object as member variable in my MainWindow class and WidgetX and WidgetY included in the MainWindow, as described above.
Question 1: Now I want to connect one of the widgets with the logic object and do the following in the constructor of the MainWindow class.
@connect(ui.widget->ui.pushButton, SIGNAL(clicked()), &logic, SLOT(doStuff()));@
It doesn't compile since the member WidgetX::ui is private. I have no idea what to do against this.
Question 2: Is there a better aproach than mine?
If you're going to say read the model/view programming article: I find it hard to understand and also it only revolves around the representation of data (e.g. databases or filesystems) which didn't bring me any further.
I want to do this as clean as possible and have googled a lot but didn't find an answer, so i'd even be happy if you could provide me some keywords to google for.
AlexanderL last edited by
Update: I've figured out what my problem was. I have to do the connects in the constructor of the corresponding object itself, not in the MainWindow class. To avoid that there are multiple instances of my logic object (which would probably cause troube since it does network i/o) i used the "singleton pattern":http://www.qtcentre.org/wiki/index.php?title=Singleton_pattern#Template-based_Approach .
So in every custom widgets constructor i do the following: @Logic *logic = &Singleton<Logic>::Instance();@
I think the approach is okay and so far, it works.
But still, if you know better design approaches, i'd be happy to hear them!