Results 1 to 8 of 8

Thread: Tracking Drag & Drop mouse cursor position?

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Tracking Drag & Drop mouse cursor position?

    I am trying to expand an existing drag & drop implementation. When dragging a row element from one QTableWidget to another, I need to be able to identify the type of object the row under the mouse cursor represents to determine if a drop is allowed.

    For example, I am dragging a row representing an object of type X. The drop table contains objects of type X, Y and Z. When the drag cursor is above objects NOT of type X the cursor should change and a drop not allowed.

    Using dragEnterEvent() and dragMoveEvent() I can tell what the row type is when the cursor enters the QTableWidget but not when it moves to another row in the same table. How can I track the position of the curson during a drag operation?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Tracking Drag & Drop mouse cursor position?

    Quote Originally Posted by mclark
    Using dragEnterEvent() and dragMoveEvent() I can tell what the row type is when the cursor enters the QTableWidget but not when it moves to another row in the same table. How can I track the position of the curson during a drag operation?
    dragMoveEvent does allow what you seek. Use the indexAt() method to get the item index. It contains both the row and column number of the hovered item.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking Drag & Drop mouse cursor position?

    dragMoveEvent does allow what you seek. Use the indexAt() method to get the item index.
    I cannot find an available function called indexAt() for QDragMoveEvent, although pos() will give me the mouse coordinates as a QPoint object.

  4. #4
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking Drag & Drop mouse cursor position?

    I found what you were referring to. Many thanks for your help.

  5. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking Drag & Drop mouse cursor position?

    The cursor changes to Qt::ForbiddenCursor when the mouse cursor crosses the row or column header of another QTableWidget. I does not change back while in that table. If entering the table from the right or bottom, a drop can be completed because the cursor is the regular drag cursor but not when the Qt::ForbiddenCursor is showing.

    How can the cursor be changed back once the mouse has finished passing over the row or column header?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Tracking Drag & Drop mouse cursor position?

    Did you reimplement dragEnterEvent? Can you show me the code you wrote concerning dragging?

  7. #7
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking Drag & Drop mouse cursor position?

    These are the implementations of dragEnterEvent() and dragMoveEvent() I'm working with:

    Qt Code:
    1. void MyTable::dragEnterEvent( QDragEnterEvent* pEvent )
    2. {
    3. if ( pEvent->mimeData()->hasFormat( MY_MIME_TYPE ) )
    4. {
    5. if ( pEvent->source() == this ) // same table, move entry
    6. {
    7. pevent->setDropAction( Qt::MoveAction );
    8. pevent->accept();
    9. }
    10. else // different table, add entry
    11. {
    12. QTableWidgetItem* pItem = itemAt( pEvent->pos() );
    13. if ( pItem != 0 )
    14. {
    15. int nDropRow = row( pItem );
    16. if ( nDropRow != -1 )
    17. {
    18. QString sType = item( nDropRow, COL_DeviceType )->text();
    19.  
    20. if ( sType.compare( m_parent->m_sDragDeviceType ) != 0 )
    21. pEvent->ignore();
    22. }
    23. }
    24. else
    25. pEvent->acceptProposedAction();
    26. }
    27. }
    28. else
    29. pEvent->ignore();
    30. }
    31.  
    32. void MyTable::dragMoveEvent( QDragMoveEvent* pEvent )
    33. {
    34. if ( pEvent->mimeData()->hasFormat( MY_MIME_TYPE ) )
    35. pEvent->acceptProposedAction();
    36. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking Drag & Drop mouse cursor position?

    Solved this one. Some debugging code was biting me in the backside ! Thanks for the help wysota!

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 21:36
  2. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 16:37
  3. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 17:41
  4. drag and drop
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2006, 12:46
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 17: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.