Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag and Drop From/To Desktop

Drag and Drop From/To Desktop

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scayetanot
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        scayetanot
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved