Results 1 to 8 of 8

Thread: Questions about Drag and Drop

  1. #1
    Join Date
    May 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12

    Default Questions about Drag and Drop

    Hi,

    I've overridden the functions dragEnterEvent and dropEvent for a class which inherits from QTableWidget. This class is set to accept drops, and I have an instance of it in my main form.

    First, in my dragEnterEvent, I'd like to check that the file is of mp3 format. I tried with the following:
    Qt Code:
    1. if (event->mimeData()->hasFormat(QLatin1String("audio/mpeg")))
    To copy to clipboard, switch view to plain text mode 
    But this always seems to fail. In fact, no format has worked thus far, and I'm not sure why. Is there something I'm missing or doing wrong?

    I then decided that as an alternative method, I'd get the file name and extract the file format myself. My question here is how exactly I can retrieve either the File object itself, or just the filename from within these functions (I assume somehow from the QDragEnterEvent and QDropEvent).

    Thanks

  2. #2
    Join Date
    May 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12

    Default Re: Questions about Drag and Drop

    Bump....

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Questions about Drag and Drop

    Dropping from where exactly? Another object on the same form? Different format? Different application? Desktop?

  4. #4
    Join Date
    May 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12

    Default Re: Questions about Drag and Drop

    From windows file explorer, for example, onto my form.

  5. #5
    Join Date
    Jul 2009
    Posts
    139
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 59 Times in 52 Posts

    Default Re: Questions about Drag and Drop

    Try iterating the formats list:
    Qt Code:
    1. void Test11::dragEnterEvent ( QDragEnterEvent * event )
    2. {
    3. for (int i = 0; i < event->mimeData()->formats().count(); i++)
    4. {
    5. qDebug() << event->mimeData()->formats().at(i);
    6. }
    7.  
    8. if (event->mimeData()->hasUrls())
    9. {
    10. for (int i = 0; i < event->mimeData()->urls().count(); i++)
    11. {
    12. qDebug() << event->mimeData()->urls().at(i).toString();
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to numbat for this useful post:

    Polnareff (2nd June 2010)

  7. #6
    Join Date
    May 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12

    Default Re: Questions about Drag and Drop

    The first loop yielded

    "application/x-qt-windows-mime;value="Shell IDList Array""
    "application/x-qt-windows-mime;value="DragImageBits""
    "application/x-qt-windows-mime;value="DragContext""
    "application/x-qt-windows-mime;value="InShellDragLoop""
    "text/uri-list"
    "application/x-qt-windows-mime;value="FileName""
    "application/x-qt-windows-mime;value="FileNameW""

    And the second:

    "file:///C:/Documents and Settings/amaior/Desktop/MixTape/Test.mp3"

    I suppose I can just use the second then. Thanks

  8. #7
    Join Date
    May 2010
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12

    Default Re: Questions about Drag and Drop

    So now my problem is that I never enter the dropEvent function. My constructor specifies setAcceptDrops(true), and I've tried calling both accept() and acceptProposedAction(), even right at the beginning of dragEnterEvent. No matter what, when dragging in a file, the mouse cursor continues to be the null cursor, and it never enters my dropEvent. Am I missing something?

  9. #8
    Join Date
    Jul 2009
    Posts
    139
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 59 Times in 52 Posts

    Default Re: Questions about Drag and Drop

    The docs say:
    Subclassing Complex Widgets

    Certain standard Qt widgets provide their own support for drag and drop. When subclassing these widgets, it may be necessary to reimplement dragMoveEvent() in addition to dragEnterEvent() and dropEvent() to prevent the base class from providing default drag and drop handling, and to handle any special cases you are interested in.

  10. The following user says thank you to numbat for this useful post:

    Polnareff (2nd June 2010)

Similar Threads

  1. A few questions
    By T3AB4GG3R117 in forum Newbie
    Replies: 2
    Last Post: 21st January 2009, 09:01
  2. KDE + Qt --- Questions
    By nupul in forum KDE Forum
    Replies: 2
    Last Post: 23rd April 2006, 19: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
  •  
Qt is a trademark of The Qt Company.