Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem parent child event handling
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem parent child event handling

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsitem
3 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Glenn94
    wrote on last edited by Glenn94
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      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);
      
      G 1 Reply Last reply
      0
      • A Asperamanca

        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);
        
        G Offline
        G Offline
        Glenn94
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved