[QT] QT realizes the function of dragging and copying files

QT realizes the function of dragging and copying files

Catalog

1. Add header file

2. Add. h file code

3. Add. cpp file code

Copy files and folders

Set mouse hover style

4. effect

1. Add header file

#include <QDragEnterEvent>
#include <QUrl>
#include <QMimeData>
#include <QDropEvent>
#include <QDrag>
#include <QRect>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>

2. Add. h file code

protected:
    void dragEnterEvent(QDragEnterEvent *event);    //Drag into event
    void dropEvent(QDropEvent *event);              //Drop events

private:
    bool copyFiles(QString sourceFile,QString destFile);
    bool copyDir(QString sourceDir,QString destDir);

3. Add. cpp file code

//Drag and drop
void Softdog::dragEnterEvent(QDragEnterEvent *event)
{
    if(event->mimeData()->hasUrls()){
        event->acceptProposedAction();
    }
    else{
        event->ignore();
    }
}
void Softdog::dropEvent(QDropEvent *event)
{
    const QMimeData *mimedata = event->mimeData();
    if(mimedata->hasUrls()){
        QList<QUrl>urlList = mimedata->urls();
        if(urlList.isEmpty()){
            return ;
        }
        QString fileName = urlList.at(0).toLocalFile();
        qDebug() << fileName;
        if(fileName.isEmpty()){
            return ;
        }
        QFileInfo fileInfo(fileName);
        if(fileInfo.isFile()){
        QFile file(fileName);

//            qDebug() << tr("%1-%2").arg(fileName).arg("DND File");
            if(!file.open(QIODevice::ReadOnly)){
                return ;
            }

//            QTextStream in(&file);
//            ui->label_addfiles->setText(in.readAll());
            bool res =   copyFiles(fileName,privateSum);
            qDebug() << res;
        }
        else if(fileInfo.isDir()){
            bool res =   copyDir(fileName,privateSum);
            qDebug() << res;
        }
    }

}

Copy files and folders

bool Softdog::copyFiles(QString sourceFile, QString destFile)
{
    if(sourceFile == destFile ){
        return true;
    }
    if(!QFile::exists(sourceFile)){
        return false;
    }
//    if(!QFile::copy(sourceFile,destFile)){
//        qDebug() << "source:" << sourceFile << ";";
//        qDebug() << "dest:" << destFile << ";";
//        QFile file(sourceFile);
//        file.remove(destFile);
//        return false;
//    }
    QProcess *pro1=new QProcess;
    pro1->start("cp \""+sourceFile+"\" "+destFile);  //If there is a space in the filename, you must add double quotes around the filename + suffix. If you are in quotes, you need to add escape characters and a slash\

    return true;
}
bool Softdog::copyDir(QString sourceDir, QString destDir)
{
    if(sourceDir == destDir ){
        return true;
    }


//    if(!QFile::copy(sourceFile,destFile)){
//        qDebug() << "source:" << sourceFile << ";";
//        qDebug() << "dest:" << destFile << ";";
//        QFile file(sourceFile);
//        file.remove(destFile);
//        return false;
//    }

    QProcess *pro1=new QProcess;
    pro1->start("cp -r "+sourceDir+" "+destDir);

    return true;
}

Set mouse hover style

   //Drag and drop area styles
    ui->label_addfiles->setStyleSheet("QLabel{background:#FFFFFF;}"
                                      "QLabel:hover{color:red;"
                                      "background:#FFFFE1;}"
                                      );

4. effect

141 original articles published, 24 praised, 60000 visitors+
Private letter follow

Keywords: Qt

Added by legio on Mon, 20 Jan 2020 20:06:13 +0200