Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT: Is it a good idea to base my domain objects on QObject?

Tags:

c++

qt

qobject

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:

  • Hierarchy
  • Signals and slots
  • Properties
  • Being able to use guarded pointers
  • Internationalization

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

like image 861
StanB123 Avatar asked Jul 18 '10 14:07

StanB123


3 Answers

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).

like image 160
jsegal Avatar answered Nov 15 '22 18:11

jsegal


"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.

like image 44
Alex F Avatar answered Nov 15 '22 18:11

Alex F


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.

like image 42
Gianni Avatar answered Nov 15 '22 19:11

Gianni