Results 1 to 4 of 4

Thread: QMimeData->hasText() returning true for non-text files

  1. #1
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QMimeData->hasText() returning true for non-text files

    I'm trying to implement some Drag and Drop functionality in a program, and I only want to accept text files. Based off an example I found online I'm using the following funciton

    Qt Code:
    1. void MainWindow::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (event->mimeData()->hasText())
    4. {
    5. event->acceptProposedAction();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    If I drag a text file onto it from Explorer (only tested under Windows 7 65 bit so far) the code functions correctly. Where I'm getting confused and am not finding much documentation, is if I drag a non-text file onto it such as a BMP, PNG, PDF (the only three binary files I've tested with so far) the hasText() functions returns true. Any one have any suggestions as to what I may be doing wrong?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMimeData->hasText() returning true for non-text files

    What is the text? My guess is that the text is the file name because if you drag and drop onto the Window Run dialog that is what you get.

    The QMimeData object can hold several data payloads in different formats. What does QMimeData::formats() return?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData->hasText() returning true for non-text files

    As Chris already speculated, this is a misunderstanding of what the drag contains
    The drag object contains the file URIs and URIs are text.

    So you will have to get the content of the drag and then perform checks on the received files, e.g. by extension or using MIME magic.

    For the latter Qt5 has QMimeDatabase and I think I've heard about some third party backport to Qt4.

    Cheers,
    _

  4. #4
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMimeData->hasText() returning true for non-text files

    Thanks for your help. That makes sense, I wasn't thinking about the file name being included as part of the hasText function.

Similar Threads

  1. QMimeData using setText and setUrls at the same time
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2011, 01:25
  2. How to use QMimeData::setHtml?
    By Vadi in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2011, 17:52
  3. Is there a known problem with QMimeData on Mac OS X?
    By Wurgl in forum Qt Programming
    Replies: 8
    Last Post: 27th February 2008, 23:21
  4. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 01:06
  5. MIME database and QMimedata
    By nupul in forum Qt Programming
    Replies: 12
    Last Post: 12th April 2006, 15:36

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.