"Theoretical" question: How to nicely expose and edit properties of several classes?
I have been working on something like a settings-editor to let the user configure part of a program. But even after lots of thinking I don't know a pretty solution for how to actually access and set the properties of various classes (mostly widgets) via the editor. I looked through a couple of books, the whole Internet (did'nt know the right key-words)... maybe somebody with more experience can help me.
class A contains an object of class B, which contains an object of class C, which contains an object of class D, which contains an object of class E. Also C contains an object of D' and that one contains one of E'.
This structure is due to the widgets being something like containers for each other, and it seemed logical (maybe it isn't, please correct me!). Now class D (or C, or D') wants to create an editor to edit all the properties of all these classes .
Would it be better to hand lots of pointers to the editor, or to just use it to retrieve user-set values and then set everything inside a method of C? This right now seems incredibly ugly to me and I would love to find a nice and clean solution.
Hopefully somebody can help me. :)
What about using QtPropertyBrowser? Is that what you mean? To have a property editor like the one in Qt Designer?
No, it's supposed to be an editor for the end-user. I created a nice widget to be able to control all the necessary settings (i.e. properties of a group of widgets), but I am not sure how to make that editor interact nicely with said group.
I don't know how to make the "editor" properly interact with the widgets (pointers to its constructor?), or how to access all the properties from within the editors methods. Everything I can come up with feels really convoluted and unstructured.
You can do it in a generic way: using "QMetaObject":http://qt-project.org/doc/qt-5/QMetaObject.html. All classes derived from QObject have an associated QMetaObject, which can be accessed to gather (at runtime) all the information about the class:
- class name
- object name
- object properties (defined with Q_PROPERTY)
- many other information
Most of those (especially properties) can also be changed.
That's not really helping me - I am not looing for a solution to a problem with Qt, but a more generic one. I have different classes and I don't know how to organize them so that I can access their properties from a central location (the "editor" for the user). A bit like the standard "settings-menu" you find in applications. There you can set things that affect all parts of the application, but how are the settings ideally transported to the classes underneath?
In my case something like global or static variables isn't possible due to the overarching structure of the program.
I don't really know how to express the problem any better. :-/
Make your settings class a singleton if you want an easy way. Or use QSettings in all your classes. Or create a value passing chain, which will be a lot of work to get to all subwidgets. To make it a bit easier, you can expose values of those subwidgets as public variables.
Or, still, the solution with QMetaObject remains valid, as far as I can understand your problem.
A value passing chain sounds interesting, could you elaborate a bit on that? Maybe I am already using something like it: a QSettings object that is handed to all children of the top-most class, and every child then calls its own implementation of something like "saveSettings()". But since that object contains a lot more information then should be editable by the user, I don't really know how to use it for the editor (loading the new settings would also reload all the non-editable settings, which would cause many a problem).
So I assume that it's not terrible practice to have a parent/child-structure like the one I described in post one?
Nope, that is perfectly fine. All solutions have their advantages and drawbacks.