Drag&Drop



  • I have custom QTableWidget, how drop lines from table to file system? File to program workd perfect

    void ArchiveQTableWidget::dragEnterEvent(QDragEnterEvent *event)
    {
        if (event->mimeData()->hasUrls()) {
            event->acceptProposedAction();
        }
    }
    
    void ArchiveQTableWidget::dragMoveEvent(QDragMoveEvent *event)
    {
        event->acceptProposedAction();
    }
    
    void ArchiveQTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
    {
        event->accept();
    }
    
    void ArchiveQTableWidget::dropEvent(QDropEvent *event)
    {
        foreach (const QUrl &url, event->mimeData()->urls()) {
            QString fileName = url.toLocalFile();
            qDebug() << "Dropped file:" << fileName;
        }
    }
    
    void ArchiveQTableWidget::startDrag(Qt::DropActions supportedActions)
    {
        qDebug() << supportedActions;
        QFile qf("1.txt");
        qf.open(QFile::WriteOnly);
        qf.write(QByteArray("dasdasdwqdd"));
        qf.close();
        QMimeData *mimeData = new QMimeData;
        mimeData->setUrls(QList<QUrl>() << QUrl(QFileInfo("1.txt").absoluteFilePath()));
        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Looks a bit like this stack overflow question.

    Hope it helps



  • I found solution for one file, how copy more that one file?

            QString filename = "1.txt";
            QMimeData* mimeData = new QMimeData;
            FILEGROUPDESCRIPTOR desc;
            desc.cItems = 1;
            desc.fgd[0].dwFlags = FD_PROGRESSUI;
            wcscpy_s(desc.fgd[0].cFileName, filename.toStdWString().c_str());
            // make a deep copy here
            mimeData->setData("FileGroupDescriptorW", QByteArray((const char*)&desc, sizeof(FILEGROUPDESCRIPTOR)));
            mimeData->setData("FileContents", QByteArray("whatadjsaofhniewfnj"));
            QDrag *drag = new QDrag(this);
            drag->setMimeData(mimeData);
            drag->exec(Qt::CopyAction | Qt::MoveAction);
    

Log in to reply
 

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