Reputation: 1315
I'm trying to move a single object subclassed from a QGraphicsSvgItem around a scene. I've looked at the Qt Drag and Drop example, but my goal isn't to drag and drop like the way they have it. Rather, I want the entire object's location to follow the mouse until it is dropped.
Is there a simple way of doing this in Qt? Thanks!
This is what I have so far:
mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length() < QApplication::startDragDistance()) {
return;
}
QDrag *drag = new QDrag(event->widget());
QMimeData *mime = new QMimeData;
drag->setMimeData(mime);
QPixmap pixmap(100, 100);
pixmap.fill(Qt::white);
QPainter painter(&pixmap);
painter.translate(15,15);
painter.setRenderHint(QPainter::Antialiasing);
paint(&painter, 0, 0);
painter.end();
pixmap.setMask(pixmap.createHeuristicMask());
drag->setPixmap(pixmap);
drag->setHotSpot(QPoint(50, 50));
drag->exec();
setCursor(Qt::OpenHandCursor);
}
Upvotes: 0
Views: 422
Reputation: 73081
If all you want is to have the object draggable by the user, you don't need to override mouseMoveEvent or anything; instead just do this:
theSVGItem->setFlag(ItemIsMovable, true);
... and the QGraphicsView will handle it for you.
Upvotes: 2