PDA

View Full Version : Questions about Drag and Drop



Polnareff
1st June 2010, 19:55
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:

if (event->mimeData()->hasFormat(QLatin1String("audio/mpeg")))
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 :)

Polnareff
2nd June 2010, 14:35
Bump.... :)

squidge
2nd June 2010, 14:37
Dropping from where exactly? Another object on the same form? Different format? Different application? Desktop?

Polnareff
2nd June 2010, 14:41
From windows file explorer, for example, onto my form.

numbat
2nd June 2010, 14:45
Try iterating the formats list:


void Test11::dragEnterEvent ( QDragEnterEvent * event )
{
for (int i = 0; i < event->mimeData()->formats().count(); i++)
{
qDebug() << event->mimeData()->formats().at(i);
}

if (event->mimeData()->hasUrls())
{
for (int i = 0; i < event->mimeData()->urls().count(); i++)
{
qDebug() << event->mimeData()->urls().at(i).toString();
}
}
}

Polnareff
2nd June 2010, 14:53
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 :)

Polnareff
2nd June 2010, 15:59
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?

numbat
2nd June 2010, 16:29
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.