Results 1 to 4 of 4

Thread: Drag and Drop without subclass a widget.

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Question Drag and Drop without subclass a widget.

    Hi to all,
    I've a form that contains Qlinedits, Qlabels and a QListWidget. All the components are well positioned on a form, but now I've to manage the Drag and Drop of files on that QListWidget. So, I read about that issue and the only examples I've found are about subclassing the target widget. Is it possible to manage the Drag and Drop without subclassing(and manually handle the layout) the QListWidget?
    Thanks.

  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: Drag and Drop without subclass a widget.

    Yes it is indeed possible, using event filters.
    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. The following user says thank you to Santosh Reddy for this useful post:

    cydside (12th January 2013)

  4. #3
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag and Drop without subclass a widget.

    Could you please post a pseudo code or suggest an implementation?

  5. #4
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Drag and Drop without subclass a widget.

    OK, solved. Here is the job I've done:

    Qt Code:
    1. // Form Header (frmSetBackupDir class)
    2.  
    3. // Added
    4. protected:
    5. bool eventFilter(QObject *obj, QEvent *event);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Form Implementation
    2.  
    3. // Added in the CTOR
    4. ui->lneDirBU->installEventFilter(this);
    5. ui->lneDirBU->setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool frmSetBackupDir::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (event->type() == QEvent::DragEnter)
    4. {
    5. QDragEnterEvent *tDragEnterEvent = static_cast<QDragEnterEvent *>(event);
    6. tDragEnterEvent->acceptProposedAction();
    7.  
    8. return true;
    9. }
    10. else if (event->type() == QEvent::DragMove)
    11. {
    12. QDragMoveEvent *tDragMoveEvent = static_cast<QDragMoveEvent *>(event);
    13. tDragMoveEvent->acceptProposedAction();
    14.  
    15. return true;
    16. }
    17. else if (event->type() == QEvent::Drop)
    18. {
    19. QDropEvent *tDropEvent = static_cast<QDropEvent *>(event);
    20. tDropEvent->acceptProposedAction();
    21.  
    22. qDebug() << "OK, execute your task!";
    23.  
    24. return true;
    25. }
    26. else
    27. {
    28. // standard event processing
    29. return QObject::eventFilter(obj, event);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    It seems to work. Better implementations are well accepted!

Similar Threads

  1. Replies: 1
    Last Post: 12th September 2012, 19:26
  2. Drag and drop inside and outside a Widget
    By utopia500 in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 13:42
  3. QListWidgetItem subclass - Crash program in drag/drop
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 09:24
  4. Drag and Drop a widget in and out QTabWidget
    By smarinr in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2008, 19:10
  5. Drag and Drop in a QTreeWidget subclass
    By kishore in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2008, 07:12

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.