
Originally Posted by
kleimola
If setting a custom pixmap with QDrag's setPixmap(const QPixmap &pixmap) it will follow the drag only for a very short moment and then stop (with artifacts). It also starts by flashing a black background. The drag operation itself works ok. This can be easily seen by compiling the 'puzzle' drag and drop example. Hopefully this will be fixed in the final release. I tried on 4.6.0~git20091218-0maemo1.
Seems that the convenience functionality of movable items with QGraphicsItem::setFlag( ItemIsMovable ) works without problems though. So if the item's pixmap itself is ok as the dragging icon, then reimplementing the mousePress, mouseRelease and mouseMove in e.g. scene for more control will give similar behavior as with custom QDrag (not the MimeData functionality though I think). When using this approach you can skip all the dragEnter, dragLeave, dragMove and dropEvent functions (they seem not to be called without custom QDrag exec?). Since the default convenience functionality starts moving the item immediately (which may be unwanted with Maemo5's finger UI) you can delay it and even separate mouse press from move with this simple reference code example in a reimplemented QGraphicsScene
Code:
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->buttons() != Qt::NoButton && ourItem.isSelected()) {
if((mouseEvent->screenPos() - mouseEvent->buttonDownScreenPos(Qt::LeftButton))
.manhattanLength() > QApplication::startDragDistance()) {
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
}
}
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (...) {
// Don't dispatch mousePressEvent to the item if we don't want to move it
}
else
QGraphicsScene::mousePressEvent(mouseEvent);
}
void MyScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
// Let QGraphicsItem handle mouseRelease first (otherwise our setPos() won't work)
QGraphicsScene::mouseReleaseEvent(mouseEvent);
if (...) {
// If we need to cancel the drag or similar, we can reposition the item here
ourItem->setPos(some_X_otherthanDragResult, some_Y_otherthanDragResult);
}
}
Let me know if you know a better way or there's something strange here. I wonder if the default convenience functionality is faster/lighter than reimplementing everything with QDrag and the dragevents (when they will eventually work)?
>> Johannes <<