I'm reasonably new to using the QT framework in combination with C++. I was wondering: Is it a good idea to base my domain classes on QObject? Or should I only do this for classes higher up in the hierarchy? (closer to the user interface level). The QT documentation isn't clear on this:
Taken from the QT documentation:
The meta-object system is a C++ extension that makes the language better suited to true component GUI programming.
Obviously I want to build my application in a nice well structured way. Over the past few days I have been browsing through the QT documentation in order to find an answer to this question. I don't want to make some elementary mistake which will make my application limp for all eternity ;-).
I have already looked at the basic documentation for QObject and the Qt Object model. I also found a freshmeat article which helped but didn't really help me reach a conclusion. Something else that confuses me is that QT itself doesn't seem to be consistent on this matter because not all QT classes use QObject as a base class.
The advantages of using QObject as a base class as I see them:
However, I don't require any of these functionalities in most of my domain classes. Is there a best practise rule for this? Or should the rule be: use it if you require any of the points mentioned above?
Hope I didn't make this too confusing :-)
In general, unless there is a "compelling need," you are better off keeping your domain classes "vanilla." That gives you the most flexibility in the future (e.g. re-using them in a non-Qt environment).
"Use it if you require any of the points mentioned above" - it is difficult to say better. There is no reason to add unnecessary functionality to every class. Think also about classes defined in shared libraries: they can be used by non-Qt clients, if you don't derive them from QObject.
This issue isn't 'as big' as you might think. It really doesn't matter all that much. I'd say if you do or don't it really won't be all that different. So, as a rule of thumb, don't, just to make things simpler. If, however, you need signal-slots or anything Qt realated, go ahead, it doesn't cost all that much anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With