Results 1 to 7 of 7

Thread: Can't catch DragLeaveEvent

  1. #1
    Join Date
    Mar 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't catch DragLeaveEvent

    I have a QDialog with two QListWidgets. The user can copy items from the first list (availableItemsList) to the second (myList) via drag and drop. There he can reorder them and then confirm the dialog. I would like the user to be able to remove an item from the second list via drag and drop by simply dropping it outside the list widget; so far this is only possible by selecting the item and hitting delete.

    The idea was to remove an item on receiving the DragLeaveEvent. To realize this, I have tried installing an event filter on the list widget (myList), but the DragLeaveEvent is never fired, even though the mouse cursor changes to the "not allowed" sign when an item is dragged outside myList, and neither is the DragEnterEvent fired every time I would expect it to be, not even when I drag a new item from the availableItemsList to myList. Here is my code (so far only qDebug output, no removal of the item):

    Qt Code:
    1. MyDialog::MyDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::MyDialog)
    4. {
    5. /* ... */
    6. ui->myList->installEventFilter(this);
    7. }
    8.  
    9. bool MyDialog::eventFilter(QObject *obj, QEvent *event)
    10. {
    11. qDebug() << "Event Filter called with " << event->type();
    12. if (event->type() == QEvent::DragLeave) {
    13. qDebug() << event->type() << "= drag leave event.";
    14. return true;
    15. } else if (event->type() == QEvent::DragEnter) {
    16. qDebug() << event->type() << "= drag enter event.";
    17. return true;
    18. } else {
    19. // standard event processing
    20. return QObject::eventFilter(obj, event);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    List properties (dialog created with Qt Designer):

    List 1 (availableItemsList):
    - acceptDrops: false
    - dragEnabled: false
    - dragDropOverwriteMode: false
    - dragDropMode: DragOnly
    - defaultDropAction: IgnoreAction

    List 2 (myList):
    - acceptDrops: true (if value is set to false, nothing changes)
    - dragEnabled: true (if value is set to false, nothing changes)
    - dragDropOverwriteMode: true
    - dragDropMode: DragDrop
    - defaultDropAction: MoveAction

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    You are not checking for the obj receiving the event.
    This should not be a problem, but see if you get at all events from your list:
    Qt Code:
    1. bool MyDialog::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. qDebug() << "Event Filter called with " << event->type();
    4. if(obj == ui->myList)
    5. {
    6. if (event->type() == QEvent::DragLeave) {
    7. qDebug() << event->type() << "= drag leave event.";
    8. return true;
    9. } else if (event->type() == QEvent::DragEnter) {
    10. qDebug() << event->type() << "= drag enter event.";
    11. return true;
    12. }
    13. }
    14. else {
    15. // standard event processing
    16. return QObject::eventFilter(obj, event);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    Hi, I changed my code the way you suggested, but I don't see that it changed much with regard to the output I get. Specifically, I still don't get the dragLeaveEvent. I get the regular Enter and Leave events fine (10 and 11 respectively, see here), but not the dragEnter and dragLeave events (60 and 62).

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    The idea was to see if you are getting ANY events from this object.
    Put a debug output before the first if(event()->type()...) statement and see if it gets printed out.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    Sorry, I was being unprecise. I changed my code to the following:

    Qt Code:
    1. bool MyDialog::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == ui->myList) {
    4. qDebug() << "Event Filter called with " << event->type();
    5. if (event->type() == QEvent::DragLeave) {
    6. qDebug() << event->type() << "= drag leave event.";
    7. return true;
    8. } else if (event->type() == QEvent::DragEnter) {
    9. qDebug() << event->type() << "= drag enter event.";
    10. return true;
    11. } else if (event->type() == QEvent::Drop) {
    12. qDebug() << event->type() << "= drop event.";
    13. return true;
    14. }
    15. } else {
    16. // standard event processing
    17. return QObject::eventFilter(obj, event);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Result was as described above.

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    Two things:

    1) List must have DragDropMode set to DragDrop or InternalMove, otherwise drag Enter/Leave events will not be delivered (don't ask why).
    2) For another reason I don't know you can't reliably intercept drag enter/leave events using event filter. Subclass QListWidget and reimplement dragEnterEvent() and dragLeaveEvent().

    Qt Code:
    1. class MyList : public QListWidget
    2. {
    3. public:
    4. MyList( QWidget* parent = 0 ) : QListWidget( parent ) {}
    5.  
    6. void dragEnterEvent( QDragEnterEvent* e )
    7. {
    8. qDebug() << "Enter!";
    9. QListWidget::dragEnterEvent( e );
    10. }
    11.  
    12. void dragLeaveEvent( QDragLeaveEvent* e )
    13. {
    14. qDebug() << "Leave!";
    15. QListWidget::dragLeaveEvent( e );
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 

    Maybe threre are some flags that could alter this behavious but I don't know them.

  7. #7
    Join Date
    Mar 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't catch DragLeaveEvent

    Yeah, I haven't found any way around subclassing either, that's why I posted here. Thanks for your help everyone!

Similar Threads

  1. how to catch qMouseEvent
    By pakine in forum Newbie
    Replies: 4
    Last Post: 27th June 2012, 11:47
  2. Catch a signal
    By Archa4 in forum Newbie
    Replies: 2
    Last Post: 17th February 2011, 10:46
  3. how to catch qMouseEvent
    By pakine in forum Newbie
    Replies: 0
    Last Post: 5th July 2010, 18:16
  4. [QGraphicsItem] isMovable && dragLeaveEvent
    By Spitz in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2008, 10:55
  5. How to catch segfaults?
    By Daliphant in forum Newbie
    Replies: 4
    Last Post: 3rd July 2008, 15:17

Tags for this Thread

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.