QGraphicsItem parent child event handling



  • Hi all,

    I wan to make a custom QGraphicsItem that concists of the following parts:
    9 draggable "handles" and 3 rectangle fields. :
    0_1525700693103_d44b1633-700c-43d2-b719-3e1586bf7719-image.png

    I've been trying for some time now, but i'm stuck on getting updates on child events. I've made a GripBox(child) and a SensorField(parent) class. The GripBox class functions as a grabber for resizing the field. I hope someone can help me with some directions on how to establish some sort of interface between parent and child items.

    I've started out with one field in the code below as i don't know yet how to make sure that the red field remains the topfield, and all the fields remain in the yellow field.

    GripBox.cpp

    #include "gripbox.h"
    #include "squaresensorfield.h"
    #include <QPainter>
    #include <QStyle>
    #include <QStyleOption>
    #include <QDebug>
    
    
    
    GripBox::GripBox(QGraphicsItem *parent)
        : QGraphicsItem(parent)
    {
        _pressed = false;
        position=QPointF(0,0);
        //setFlag(ItemIsMovable);
        setFlag(ItemSendsGeometryChanges);
        setFlag(ItemIsSelectable);
        initComplete=false;
    }
    
    QRectF GripBox::boundingRect() const
    {
        //define center and outer corners
        return(QRectF(-5,-5,10,10));
    }
    
    void GripBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        QRectF rect = boundingRect();
        QRadialGradient gradient(-3, -3, 10);
        if (option->state & QStyle::State_Sunken) {
            gradient.setCenter(3, 3);
            gradient.setFocalPoint(3, 3);
                gradient.setColorAt(1, QColor(Qt::green).light(120));
                gradient.setColorAt(0, QColor(Qt::darkGreen).light(120));
            }
        else {
    
            gradient.setColorAt(0, Qt::green);
            gradient.setColorAt(1, Qt::darkGreen);
        }
        painter->setBrush(gradient);
    
        painter->setPen(QPen(Qt::black, 0));
        painter->drawRect(rect);
    
    
    }
    
    QPointF GripBox::getPos()
    {
        return(position);
    }
    
    QVariant GripBox::itemChange(GraphicsItemChange change, const QVariant &value)
    {
    
        switch (change) {
        case ItemPositionHasChanged:
        default:
            break;
        };
        return QGraphicsItem::itemChange(change, value);
    }
    
    
    void GripBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        _pressed= true;
        qDebug() << "i'm pressed!";
        update();
        //QGraphicsItem::mousePressEvent(event); // zorgt ervoor dat de parents niet het klikken overriden
    }
    
    void GripBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        _pressed = false;
        update();
        //QGraphicsItem::mouseReleaseEvent(event);
    }
    void GripBox::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
        qDebug()<<"moving!";
        QGraphicsItem::mouseMoveEvent(event);
    }
    

    SquareSensorField.cpp

    #include "squaresensorfield.h"
    #include "gripbox.h"
    #include <qDebug>
    
    SquareSensorField::SquareSensorField(int width,int height)
    {
        initFlag=false;
    
        //setFlag(ItemIsMovable);
        //setFlag(ItemIsSelectable);
        setFlag(ItemSendsGeometryChanges);
        setCacheMode(DeviceCoordinateCache);
        _w = width;
        _h = height;
    
        handleLeft = new GripBox(this);
        myGrips.append(handleLeft);
        handleTop = new GripBox(this);
        myGrips.append(handleTop);
        handleRight =new GripBox(this);
        myGrips.append(handleRight);
    
        handleLeft->setPos(-0.5*_w,0);
        handleTop->setPos(0,0.5*_h);
        handleRight->setPos(0.5*_w,0);
        initFlag=true;
    
    }
    
    QRectF SquareSensorField::boundingRect() const
    {
        return(QRectF(-0.5 *_w,-0.5 *_h,_w,_h));
        //return(QRectF(0,0,100,100));
    }
    
    void SquareSensorField::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    
        QRectF rect = boundingRect();
        painter->setBrush(QBrush(Qt::transparent));
        painter->setPen(QPen(Qt::black, 2));
        painter->drawRect(rect);
    
    }
    
    void SquareSensorField::eventHandle()
    {
        //qDebug()<<""; // can't acces data from sensorfield class
    }
    
    QVariant SquareSensorField::itemChange(GraphicsItemChange change, const QVariant &value)
    {
        qDebug()<<"item change";
    
        return QGraphicsItem::itemChange(change, value);
    }
    
    void SquareSensorField::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug()<<"parent moving!";
        if (handleLeft->_pressed==true){ // ! not working
            qDebug()<<"left handle is pressed";
        }
        //QGraphicsItem::mouseMoveEvent(event);
    }
    

    result:
    0_1525701839068_6b9bb13e-cfb9-4611-aa87-653353ba47a9-image.png

    Thanks in advance!



  • I've made something very similar. In my case, the easiest was to make the handle class (GripBox in your case) a QGraphicsObject, and notify the parent via signals:

    signals:
        void notifyHandleDragMoveStarting();
        void notifyHandleDragMoveFinished();
        // pHandle is always 'this', but it's more convenient for the recipient than casting from QObject::sender()
        void notifyHandleMoved(CTpeg_GraphicsHandlePoint* pHandle,
                               const qreal dx,
                               const qreal dy);
        void notifyHandleDoubleClicked(CTpeg_GraphicsHandlePoint* pHandle);
    


  • @Asperamanca
    Thanks for the reply, I fixed it by seperating the problem instead of making one compound Qgraphicsitem, which was the goal.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.