implement drag and drop to file browser or desktop?



  • hello

    I want to implement a program (windows only), such that a person can perform drag and drop from my qt program to desktop (or explorer).

    the thing that needs to be dropped is a fake file. i.e. the file exists in memory, when dropping happens, a fake name will be created on the desktop with the data dumped from memory into the file.

    I have found some ideas for QT4, but I tried with Qt5, doesn't really work

    http://stackoverflow.com/questions/2724252/qt-4-x-how-to-implement-drag-and-drop-onto-the-desktop-or-into-a-folder

    most of the ideas suggest using application/octet-stream as mime type. but if it is the only mime type, qt doesn't allow dropping.

    only when text/uri-list is also included as a mime type, it allows dropping. but it only supports dropping an actual existing file, not a fake file with a piece of data in the memory.

    here are some of my current codes:

    one approach I tried is from here
    http://stackoverflow.com/questions/24236768/how-can-i-drag-to-the-desktop-with-qt

        QDrag *drag = new QDrag(this);
        QString filename("file:///haha.dat");
        QMimeData *mimeData = new QMimeData;
        mimeData->setData(QString::fromUtf8("text/uri-list"),filename.toUtf8());
        QByteArray data;
        data.append(filename);
        mimeData->setData("application/octet-stream", data);
    
        drag->setMimeData(mimeData);
    

    this doesn't work, no file can be dropped.

    another way is implementing my own QMimeData, but doesn't work either

    MyMimeData::MyMimeData()
    {
      list << "application/x-qt-windows-mime;value=\"Shell IDList Array\"";
      list << "application/x-qt-windows-mime;value=\"DragImageBits\"";
      list << "application/x-qt-windows-mime;value=\"DragContext\"";
      list << "application/x-qt-windows-mime;value=\"DragSourceHelperFlags\"";
      list << "application/x-qt-windows-mime;value=\"InShellDragLoop\"";
      list << "text/uri-list";
      list << "application/octet-stream";
      list << "application/x-qt-windows-mime;value=\"FileName\"";
      list << "application/x-qt-windows-mime;value=\"FileContents\"";
      list << "application/x-qt-windows-mime;value=\"FileNameW\"";
      list << "application/x-qt-windows-mime;value=\"FileGroupDescriptorW\"";
    }
    
    QVariant MyMimeData::retrieveData(const QString &mimetype, QVariant::Type preferredType) const
    {
        qDebug() << "retrieve data" << mimetype << preferredType;
        if (mimetype == "text/uri-list")
        {
            QVariantList urls;
            urls << QUrl::fromUserInput("file:///haha.dat");
            return ( urls);
        }
        return QString("haha");
    }
    
    bool MyMimeData::hasFormat(const QString &mimetype) const
    {
        for(int i = 0;i<list.size();++i)
        {
            if (list[i] == mimetype)
            {
                return true;
            }
        }
        return false;
    }
    
    QStringList MyMimeData::formats() const
    {
        return list;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.