PDA

View Full Version : QMimeData->hasText() returning true for non-text files



deusinvictus
28th October 2013, 20:17
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



void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasText())
{
event->acceptProposedAction();
}
}


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?

ChrisW67
28th October 2013, 20:53
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?

anda_skoa
29th October 2013, 07:43
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,
_

deusinvictus
29th October 2013, 15:00
Thanks for your help. That makes sense, I wasn't thinking about the file name being included as part of the hasText function.