Drag and Drop From/To Desktop



  • hi,

    I'm developping an app to transfer app between a Mac and and Android device.

    I can drag files from Finder to the app I develop to transfer pics for example but I wasn't be able to triggered the event when I'm dragging file from the app to the finder.

    The app is defined by a QTreeWidget

    @class TreeView : public QTreeWidget
    {
    Q_OBJECT
    ....@

    I have overloaded the function below :

    @void TreeView::dragEnterEvent(QDragEnterEvent *event)
    {
    qDebug() << "Drag enter event";

    const QMimeData* mimeData = event->mimeData();
    
    setBackgroundRole(QPalette::Highlight);
    if(mimeData->hasUrls()) {
        event->acceptProposedAction();
      //  emit changed(mimeData);
    }
    

    }

    void TreeView::dropEvent(QDropEvent event)
    {
    qDebug() << "On Drop Event";
    std::cout << "On Drop Event";
    const QMimeData
    mimeData = event->mimeData();

    if (mimeData->hasUrls())
    {
        QStringList pathList;
        QList<QUrl> urlList = mimeData->urls();
        QByteArray appData;
    
        for (int i = 0; i < urlList.size() && i < 32; ++i)
        {
            pathList.append(urlList.at(i).toLocalFile&#40;&#41;);
        }
        setBackgroundRole(QPalette::Dark);
        qDebug() << "On Drop Event look for selected items";
        qDebug() << pathList[0];
    
    
        QTreeWidgetItem * item = itemAt(event->pos());
        event->setDropAction(Qt::CopyAction);
    
        if(item) {
            Dialog *MyProgress = new Dialog();
            MyProgress->WaitBoxDialog();
            QString str = item->text(0);
            m_device.CopyFilesFromLocal(pathList, str);
            qDebug() << "On Drop Event : " << str << " Selected";
            MyProgress->DestroyWaitBoxDialog();
            event->acceptProposedAction();
        }
        else {
            qDebug() << "On Drop Event : no Selected items";
            event->ignore();
        }
        DisplayFilesAndFolders(0);
    }
    else
        event->ignore();
    

    }
    void TreeView::dragMoveEvent(QDragMoveEvent * event)
    {
    qDebug() << "On Drag Move Event";
    const QMimeData* mimeData = event->mimeData();

    event->setDropAction(Qt::CopyAction);
    
    if (mimeData->hasUrls())
    {
        QTreeWidgetItem *item = itemAt(event->pos());
        if(item) {
            qDebug() << "itemat: " << item->text(0);
    

    #if 0
    setStyleSheet(QString::fromUtf8("QTreeWidget::item:hover {\n"
    "background-color: rgb(123, 45, 67);\n"
    "}"));
    #endif
    }
    event->setDropAction(Qt::CopyAction);
    event->acceptProposedAction();
    }
    else
    event->ignore();

    // dropSite = event->answerRect();
    // event->acceptProposedAction();
    }

    void TreeView::dragLeaveEvent(QDragLeaveEvent* event)
    {
    qDebug() << "On Drag Leave Event";

    //event->setDropAction(Qt::CopyAction);
    

    // event->acceptProposedAction();

    std::vector<QString>::iterator it;
    
    for(it=DnDList.begin(); it<DnDList.end(); it++) {
        QString file = (*it);
        m_device.SendFilesToLocal(file);
    }
    

    // DnDclear();
    event->accept();
    }@

    And all the items of the treeview are set with the flags:

    @item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled
    @
    Any idea, why it works from Finder to app but not from app to Finder ?


  • Moderators

    An idea which enables (a platform independent) way to copy files:

    reimplement QAbstractItemModel::mimeData() so it contains a file uri-list

    there return a custom QMimeData implementation which reimplements QMimeData::retrieveData()

    The basic idea in the QMimeData::retrieveData() implementation is

    that you create a temporary file

    write the contents to the temp file

    return the path to the temp file

    this will start a copy of the temp-file to the drop destination in the desktop.



  • I was finally able to progress but the drag/drop now is not copying the file byt c reate a file locally on the mac (desktop in my case) which contain the file name I want to move.


Log in to reply
 

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