Smooth Dragging of Items



  • I'm trying to use the mouse to drag a series of items all at the same time. So far, I have overriden the mousePressEvent, mouseMoveEvent, and mouseReleaseEvent in order to provide a nice way of dragging everything.

    However, now I want to incorporate a smooth slowdown of the items when the mouse button is released, instead of an abrupt stop.

    To do this, I've calculated the velocity of the items. However, I'm not exactly sure how to continually move the items in that direction and decay the velocity until it reaches ~0. Is there any type of animation that can do this, or should I look into timers of some sort?




  • Moderators

    velocity will be a bit tricky to compute...

    But to do the animation you can do this right after drag.exec() has returned:
    @
    ...
    Qt::DropAction dropAction = drag.exec();

    if( dropAction != Qt::IgnoreAction ) //make sure drop has been performed and not cancelled
    {
    QRect startGeom(QCursor::pos()-drag.hotSpot(), drag.pixmap().size());
    QRect endGeom(startGeom);
    endGeom.moveTopLeft(...); //calculate the desired end position

    QLabel* helperLabel = new QLabel;
    helperLabel->setWindowFlags(Qt::ToolTip);
    helperLabel->setPixmap(drag.pixmap());
    helperLabel->setGeometry( startGeom );

    QPropertyAnimation *animation = new QPropertyAnimation(helperLabel, "geometry");
    animation->setDuration( ... );
    animation->setEasingCurve(QEasingCurve::InOutQuart);
    animation->setStartValue( startGeom );
    animation->setEndValue( endGeom );
    connect(animation, SIGNAL(finished()), helperLabel, SLOT(deleteLater()));

    helperLabel->show();
    animation->start(QAbstractAnimation::DeleteWhenStopped);
    }
    @



  • The animation does seem like the way to go. Now it's just a matter of getting that velocity :) what I'm doing is saving the last 5 points from the mouseMoveEvent, and calculating the average difference between these points. However, I'm getting absurdly small numbers, so I might need to buffer in a polling timer or something like that. Would you suggest any other way of doing it?


  • Moderators

    as i said...this is very tricky.

    I would start a timer and get the cursor position (QCursor::pos()) on every timer timeout. Then create a QLine with starting point from the last recorded point to the current point. And store these lines in a list for example.
    With the lines you can also get the direction (angle) and the length for example.

    This should give you all you need to calculate the velocity.


Log in to reply
 

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