Results 1 to 2 of 2

Thread: Problem with a cursor during Drag and Drop

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with a cursor during Drag and Drop

    Hi,
    I have got a tiny problem connected to a drag and drop mechanism.
    My QWidget subclass (lets call it A) is intended to be a source of elements that are copied to other type of QWidget subclass(lets call it B), thus in A I reimplement only
    Qt Code:
    1. void PawnBox::mousePressEvent(QMouseEvent *event) {
    2. //we do not need to distinguish click from a drag
    3. //and we implement drag mechanism in a mousePressEvent
    4. QPoint clickPt = event->pos();
    5. //we recognize color of the dragged item
    6. QRect pawnArea = QRect(0,0,20,20);
    7. int pawnKind = -1; //index of a global pawn vector
    8. for(int i=0;i<4;++i) {
    9. pawnArea.setRect(25,45+i*30,20,20);
    10. if(pawnArea.contains(clickPt)) { pawnKind = i; break; }
    11. pawnArea.setRect(55,45+i*30,20,20);
    12. if(pawnArea.contains(clickPt)) { pawnKind = i+4; break; }
    13. }
    14. //if a click happened on a non-pawn area we ignore it
    15. if(pawnKind == -1) return;
    16. qDebug(qPrintable(QString::number(pawnKind)));
    17. //we assign the QColor object to a QByteArray
    18. QByteArray bytes;
    19. QDataStream out(&bytes,QIODevice::WriteOnly);
    20. out << internalVectorPtr->at(pawnKind);
    21. //we pack it
    22. QMimeData *mimeData = new QMimeData;
    23. mimeData->setData("qt-class/qcolor", bytes);
    24. //we prepare cursor graphics
    25. QPixmap draggedPawnImage = QPixmap(24,24);
    26. draggedPawnImage.fill(Qt::transparent);
    27. QPainter pixPainter(&draggedPawnImage);
    28. Pawn tmpPawn = Pawn(internalVectorPtr->at(pawnKind));
    29. MTR::Global::paintPawnAt(2,2,tmpPawn,pixPainter);
    30. //we establish QDrag object, draw cursor and embed data
    31. QDrag *drag = new QDrag(this);
    32. drag->setPixmap(draggedPawnImage);
    33. //QApplication::setOverrideCursor(QCursor(*cursorPx,10,10));
    34. drag->setDragCursor(*cursorPx,Qt::MoveAction);
    35. drag->setDragCursor(*cursorPx,Qt::CopyAction);
    36. drag->setMimeData(mimeData);
    37. drag->setHotSpot(QPoint(12,12));
    38. //and we send it
    39. Qt::DropAction dropAction = drag->exec(Qt::CopyAction,Qt::CopyAction);
    40. //QApplication::restoreOverrideCursor();
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 
    and everything is fine apart from the fact that over a pawn image instead of a small and transparent dot of *cursorPx initialized in a constructor of A:
    Qt Code:
    1. //we prepare small point-pixmap which will be our mini cursor when dragging a pawn
    2. cursorPx = new QPixmap(1,1);
    3. cursorPx->fill();
    To copy to clipboard, switch view to plain text mode 
    there is Qt::ForbiddenCursor.

    How to establish desired cursor during drag and drop?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem with a cursor during Drag and Drop

    You'll see a forbidden cursor over everything that does not accept the proposed action of QDragMoveEvent.

    Qt Code:
    1. void MyWidget::dragMoveEvent(QDragMoveEvent* event)
    2. {
    3. if (event->mimeData()->hasFormat("qt-class/qcolor"))
    4. event->acceptProposedAction();
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    mtrpoland (28th December 2007)

Similar Threads

  1. Drag and Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 16
    Last Post: 19th May 2010, 14:57
  2. Strange behavior with Drag and Drop (not always drops)
    By Mona Shokrof in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2007, 18:00
  3. Drag n Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 10:52
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 16:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.