How to implement a class that inherits from QGraphicsItem and behaves like a QGraphicsItemGroup

2

I am working on a project where I needed to inherit from the class QGraphicsItem of Qt since I needed to have QGraphicsItem with special features (modification of line size, depending on the zoom, setting the boundingRect to the form of the object to be displayed, ...).

The fact is that I have now come across the need to have a class of my own that behaves like a QGraphicsItemGroup but I have encountered problems when implementing classes paint and boundingRect of it.

Below, in order to better understand my case, I show you a class diagram of how I currently have my code:

As I said, the main problem is that when inheriting from QGraphicsItem I have to implement their paint and boundingRect methods. I followed the same pattern that uses QGraphicsItemGroup , implementing the addToGroup method but I have not managed to show anything.

The other option that I have tried is that MyGraphicsItemGroup also inherit from QGraphicsItemGroup , but with this I can not eliminate the need to implement the aforementioned methods.

This is the MyGraphicsItemGroup code:

QGraphicsItemGroup *group;

MyGraphicsItemGroup::MyGraphicsItemGroup()
{
    group = new QGraphicsItemGroup();
}

QRectF MyGraphicsItemGroup::boundingRect() const
{
    return group->boundingRect();
}

void MyGraphicsItemGroup::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    group->paint(painter, option, widget);
}

I would be very grateful if anyone could give me some idea of how to solve this problem.

Thank you very much.

    
asked by Zharios 21.09.2018 в 10:55
source

1 answer

4

The QGraphicsItemGroup functionality can simply be emulated using QGraphicItem::setParentItem() . You can have a% empty% co that functions as a group or layer.

    
answered by 21.09.2018 / 11:38
source