If anybody's still interested, I think I've found a solution to this problem. For me explorer accepts a uri list, as long as it isn't empty. The problem was finding out when the drag operation has ended. retrieveData() is not only called after the drop.
#include <QtGui>
#include <windows.h>
{
public:
WinMimeData()
: runOnce(true)
{
setUrls(QList<QUrl>()<<QUrl("tmp"));
}
{
// This is surely a hack, but how else do we find out that the drag operation has completed?
if(((unsigned short)GetAsyncKeyState(VK_LBUTTON))>1)
return QMimeData::retrieveData(mimeType, type
);
if(runOnce)
{
// Now we create the file:
if(file.open())
{
file.write("Hello!");
file.flush();
}
runOnce=false;
}
return QUrl("/"+file.
fileName());
}
private:
// these need to be mutable since retrieveData() is const
mutable bool runOnce;
};
{
{
drag->setMimeData(new WinMimeData);
drag->start(Qt::CopyAction);
}
};
int main(int argc, char *argv[])
{
Widget window;
window.show();
return app.exec();
}
#include <QtGui>
#include <windows.h>
class WinMimeData : public QMimeData
{
public:
WinMimeData()
: runOnce(true)
{
setUrls(QList<QUrl>()<<QUrl("tmp"));
}
QVariant retrieveData(const QString& mimeType, QVariant::Type type)const
{
// This is surely a hack, but how else do we find out that the drag operation has completed?
if(((unsigned short)GetAsyncKeyState(VK_LBUTTON))>1)
return QMimeData::retrieveData(mimeType, type);
if(runOnce)
{
// Now we create the file:
if(file.open())
{
file.write("Hello!");
file.flush();
}
runOnce=false;
}
return QUrl("/"+file.fileName());
}
private:
// these need to be mutable since retrieveData() is const
mutable QTemporaryFile file;
mutable bool runOnce;
};
class Widget : public QWidget
{
void mousePressEvent(QMouseEvent* event)
{
QDrag *drag = new QDrag(this);
drag->setMimeData(new WinMimeData);
drag->start(Qt::CopyAction);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget window;
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks