Results 1 to 3 of 3

Thread: QDrag seems to block mouse events.

  1. #1
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QDrag seems to block mouse events.

    Hi,

    Firstly - thanks - the amount of gold in this forum has allowed me to trawl for answers for about 6 months, and not have to post. I'm impressed!

    Unfortunately I am pretty confident I actually have an issue here. Hopefully I am being stupid, for that would mean a simple fix is in order.

    I've sub classes QTreeView, and enabled it to provide a QDrag object, on mouse move. The trouble is; once the drag is over, signals like 'entered' are no longer emitted; and this is my issue; I'd like them back.

    Below is a minimal coding example that produces the issue;

    The TreeView shows the current item highlighted under the cursor, then after the drag, this stops, and the "ItemEntered" slot is never called. (Items don't have to be dragged anywhere - the drag just has to start - and things break)

    Any help would be most appreciated.

    Thanks



    Qt Code:
    1. #include <QApplication>
    2. #include <QStandardItemModel>
    3. #include <QTreeView>
    4. #include <QMouseEvent>
    5.  
    6. //////////////////////////////////////////////////////
    7. // This is so we can provide "drag from" to the main window.
    8. class DragAndDropTreeView : public QTreeView
    9. {
    10. Q_OBJECT
    11. public:
    12. DragAndDropTreeView(QObject * parent) ;
    13.  
    14. public slots:
    15. void ItemEntered(const QModelIndex & index) ;
    16.  
    17. protected:
    18. void mousePressEvent(QMouseEvent *) ;
    19. void mouseMoveEvent(QMouseEvent *) ;
    20. void mouseReleaseEvent(QMouseEvent *) ;
    21.  
    22. QPoint _clickPoint ;
    23. int _activeItem ;
    24. bool _mouseDown ;
    25. bool _displayingShadow ;
    26. } ;
    27.  
    28. DragAndDropTreeView::DragAndDropTreeView(QObject * parent)
    29. {
    30. _mouseDown = false ;
    31. _displayingShadow = false ;
    32. setMouseTracking(true);
    33. connect(this,SIGNAL(entered(const QModelIndex &)),this,SLOT(ItemEntered(const QModelIndex &))) ;
    34. }
    35.  
    36. void DragAndDropTreeView::mousePressEvent(QMouseEvent * e)
    37. {
    38. _clickPoint = e->pos() ;
    39. setCursor(Qt::ClosedHandCursor);
    40. QTreeView::mousePressEvent(e) ;
    41. // this is ugly, and probably inefficient.
    42. _activeItem = selectedIndexes()[0].sibling(selectionModel()->currentIndex().row(),0).data().toInt() ; ;
    43. _mouseDown = true ;
    44. }
    45.  
    46. void DragAndDropTreeView::mouseMoveEvent(QMouseEvent * e)
    47. {
    48. if (!_mouseDown)
    49. {
    50. QTreeView::mouseMoveEvent(e) ;
    51. return ;
    52. }
    53. if (QLineF(e->pos(), _clickPoint).length() < QApplication::startDragDistance())
    54. {
    55. QTreeView::mouseMoveEvent(e) ;
    56. return;
    57. }
    58.  
    59. QDrag *drag = new QDrag(this) ;
    60. if (drag == NULL) return ;
    61. QMimeData *mime = new QMimeData ;
    62. if (mime == NULL) return ;
    63. drag->setMimeData(mime) ;
    64.  
    65. mime->setText(QString("Temp/%1").arg(_activeItem));
    66.  
    67. // note to the unwary !! THIS BLOCKS!
    68. drag->exec() ;
    69. QTreeView::mouseMoveEvent(e) ;
    70. }
    71.  
    72. void DragAndDropTreeView::mouseReleaseEvent(QMouseEvent * e)
    73. {
    74. _mouseDown = false ;
    75. QTreeView::mouseReleaseEvent(e) ;
    76. }
    77.  
    78. // this is called properly before any drag event - but not after.
    79. void DragAndDropTreeView::ItemEntered(const QModelIndex & index)
    80. {
    81. _displayingShadow = true ;
    82. }
    83.  
    84.  
    85. int main(int argc, char * argv[])
    86. {
    87. QApplication app(argc,argv);
    88.  
    89. DragAndDropTreeView * DDTV = new DragAndDropTreeView(NULL) ;
    90.  
    91. // set up the model to accept data.
    92. QStandardItemModel * _model = new QStandardItemModel(0, 1) ;
    93. // slap some stuff in.
    94. _model->setHeaderData(0, Qt::Horizontal, QObject::tr("Number"));
    95. for (int i = 0 ; i < 10 ; i++)
    96. {
    97. _model->insertRow(i);
    98. _model->setData(_model->index(i, 0), QString("%1").arg(i));
    99. }
    100. // show all the beauty of it
    101. DDTV->setRootIsDecorated(false) ;
    102. DDTV->setModel(_model) ;
    103. DDTV->setDragEnabled(true) ;
    104. DDTV->setSortingEnabled(true) ;
    105. DDTV->setSelectionMode(QAbstractItemView::SingleSelection) ;
    106. DDTV->show() ;
    107. return app.exec();
    108. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QDrag seems to block mouse events.

    Add this
    Qt Code:
    1. void DragAndDropTreeView::mouseMoveEvent(QMouseEvent * e)
    2. {
    3. //....
    4. _mouseDown = false; //<<<<<<<<<< Add in the end
    5. }
    6.  
    7. void DragAndDropTreeView::mouseReleaseEvent(QMouseEvent * e) //<<<<< Mouse release event will not be called when drag is released, as it will be captured by QDrag
    8. {
    9. _mouseDown = false ;
    10. QTreeView::mouseReleaseEvent(e) ;
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDrag seems to block mouse events.

    Oh - my. Thanks. Well - at least this fits into the simple fix category. Sorry to trouble you.

Similar Threads

  1. Mouse Events
    By Atomic_Sheep in forum Newbie
    Replies: 3
    Last Post: 25th April 2013, 12:44
  2. Get mouse events in MAC
    By vinayaka in forum Qt Programming
    Replies: 4
    Last Post: 31st January 2012, 08:56
  3. Replies: 3
    Last Post: 8th October 2011, 09:46
  4. Mouse Events
    By daviddoria in forum Qt Programming
    Replies: 6
    Last Post: 13th May 2008, 11:55
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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.