PDA

View Full Version : making QGraphicsItemGroup inherit from QObject



pkj
11th September 2011, 16:18
For some reason QGraphicsItemGroup doesn't inherit from QObject. It creates special handling for this class objects, when making these objects scriptable and has become a nuisance for me :( . I would like to subclass QGraphicsItemGroup so that it inherits from QObject and QGraphicsItemGroup. However that is easier said than done when looking at the implementation of QGraphicsObject ;( .
So I have two ways
1. Subclass QGraphicsItemGroup to inherit from QObject and QGraphicsItemGroup and re-implement along the lines as is done in QGrapicsObject
2. Forget about QGraphicsItemGroup, Subclass from QGraphicsObject and re-implement all the functionality of QGraphicsItemGroup so that the class behaves like QGraphicsItemGroup but is also a Qobject.
Which is the easier way. And how do I go ahead about it?

Lykurg
11th September 2011, 16:53
QGraphicsItemGroup doesn't inherit from QObject to keep it »light«.
And what is wrong with:
class MyItemGroup : public QObject, public QGraphicsItemGroup
{
Q_OBJECT
public:
MyItemGroup(QGraphicsItem * parent = 0) : QObject(0), QGraphicsItemGroup(parent) {}
// and here use signal and slots as usual
};What do you want to recode as it is done in QGraphicsObject?

pkj
11th September 2011, 18:18
I had done that. But going through the large QGraphicsObject's massive code is making me nervous about some possible hacks in QGraphicsObject class to make it work as a QObject. Although I have got it working with expected behavior(in the way you told) I will hate to go through all the testing to find it can't work. I hope you understand my nervousness :)

wysota
13th September 2011, 10:07
There are no hacks there. The only thing QGraphicsObject does more compared to QGraphicsItem is that it has a bunch of properties available. If you don't need them, you don't need to write any additional code.

dvolosnykh
1st December 2013, 10:26
I face the same need as the author of this thread. Of course, that's great to have lighter QGraphicsItem when you do not need QObject functionality. The same goes for QGraphicsItemGroup. Anyway, when you do need QObject features for group of items, it would be great to have one or, at least, be able to get one without much fuzz. I though about inheriting my group from QGraphicsObject and QGraphicsItemGroup (much like QGraphicsObject inherits QObject and QGraphicsItem). Both are based on QGraphicsItem, and it seems that this solution would work if either QGraphicsObject or QGraphicsItemGroup inherit QGraphicsItem virtually. Thus, we could get a class with group management and QGraphicsObject features enabled. Does anybody aware of any troubles with such approach?