[SOLVED] Resize QGraphicsItem with drag and drop



  • Hello,

    I have a QGraphicsItem which is a rectangle. Il would like to be able to change the width of this item with drag and drop.
    For example, when close to the right extremity, you click, hold click, you drag to the right, then drop and the rectangle's width changes.

    How could I achieve that?

    Cheers,



  • Here is what I did ...
    It works, however I have artefacts when making the item smaller ... (when I click and drag to the left to reduce the rectangle width)

    Any idea please?

    Cheers,

    @
    #include "myrectangle.h"
    #include <QPainter>
    #include <QStyleOptionGraphicsItem>
    #include <QDebug>
    #include <QGraphicsSceneMouseEvent>
    #include <QGraphicsSceneHoverEvent>

    MyRectangle::MyRectangle()
    {
    this->setFlag(QGraphicsItem::ItemIsSelectable);
    //this->setFlag(QGraphicsItem::ItemIsMovable);
    isPressed = false;
    this->setAcceptHoverEvents(true);
    this->setAcceptDrops(true);
    this->xr_initial_unchanged=60;
    this->xnewref=60;
    this->xr=60;
    this->yr=40;
    }

    MyRectangle::~MyRectangle()
    {

    }

    void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    QPen pen(Qt::white);
    pen.setWidth(2);
    painter->setPen(pen);
    QRectF rec = boundingRect();

    QBrush brush(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    
    if(isPressed){
        brush.setColor(Qt::green);
    }
    
    if(option->state & QStyle::State_Selected){
        brush.setColor(Qt::blue);
    }
    
    painter->fillRect(rec,brush);
    

    }

    void MyRectangle::mousePressEvent( QGraphicsSceneMouseEvent* e)
    {
    //mDragStartPosition = e->scenePos();
    mDragStartPosition = QCursor::pos();
    QGraphicsItem::mousePressEvent( e );
    }

    void MyRectangle::mouseMoveEvent( QGraphicsSceneMouseEvent* e)
    {
    //qDebug() << "mouse move";
    if( (e->buttons() == Qt::LeftButton) && !mDragging && dragZone )
    mDragging = true;

    if( mDragging )
        {
        xr=xnewref;
        mDragEndPosition = QCursor::pos();
        //xr+=mDragEndPosition.x()-mDragStartPosition.x();
        int a = (int)(mDragEndPosition.x()-mDragStartPosition.x())/xr_initial_unchanged;
        qDebug() << (int)(mDragEndPosition.x()-mDragStartPosition.x())/xr_initial_unchanged;
        xr+=a*xr_initial_unchanged;
    
        }
        update();
        QGraphicsItem::mouseMoveEvent( e );
    

    }

    void MyRectangle::mouseReleaseEvent( QGraphicsSceneMouseEvent* e)
    {
    qDebug() << "release";
    prepareGeometryChange();
    mDragging = false;
    //xr+=mDragEndPosition.x()-mDragStartPosition.x();
    xnewref=xr;
    update();
    QGraphicsItem::mouseReleaseEvent( e );
    }

    void MyRectangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
    {
    if(event->pos().x()/xr >= 0.8 || event->pos().x()/xr <=0.2 ){
    this->setCursor(Qt::SizeHorCursor);
    this->dragZone=true;
    }
    else{
    this->setCursor(Qt::SizeAllCursor);
    this->dragZone=false;
    }
    }

    QRectF MyRectangle::boundingRect() const
    {
    return QRectF(0,0,xr,yr);
    }
    @

    Artefact :

    !http://hpics.li/1218e1c(Artefact)!



  • http://hpics.li/1218e1c is the link to the image ...
    The rectangle at the right should disapear after the click and drag from right to left. When I minimize the window and maximize it, it works then.
    I was thinking that calling update() would refresh all the scene .



  • Adding :
    prepareGeometryChange(); before update() solved the issue ...

    Cheers,



  • Nice that you found the solution!
    Please add "[Solved]" to the thread's title.


Log in to reply
 

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