Maison  >  Article  >  développement back-end  >  Comment puis-je interrompre les opérations de copie et de renommage de fichiers dans Qt ?

Comment puis-je interrompre les opérations de copie et de renommage de fichiers dans Qt ?

Susan Sarandon
Susan Sarandonoriginal
2024-11-13 15:33:02933parcourir

How can I interrupt file copy and rename operations in Qt?

Interrupting File Copy and Rename Operations

In situations where large files need to be transferred and users require the ability to cancel the operation, the default copy() and rename() methods offered by Qt can be limiting. The challenge lies in interrupting these processes without introducing significant performance overhead or user frustration.

Querying Interruption Support in Qt

While investigating the QFile documentation, it becomes apparent that Qt does not provide a built-in mechanism for interrupting copy() or rename() operations. Therefore, it is necessary to consider custom implementation strategies to address this requirement.

Custom Implementation for Copy Interrupt

To create a non-blocking copy helper, one option is to implement a dedicated thread or utilize the main thread. In either scenario, fragmented copying is necessary, where data is transferred in chunks using a buffer. By using a buffer, it is possible to track the progress of the operation and accommodate user cancellation requests.

The core steps for implementing a custom copy helper can be described as follows:

  1. Create a QObject-derived class to handle the copy operation.
  2. Initialize properties for source path, destination path, buffer size, progress, and clean-up on cancellation.
  3. Define a begin() slot to initiate the copy process, which includes opening source and destination files and allocating a buffer for data transfer.
  4. Implement a step() slot, utilizing QMetaObject::invokeMethod() with Qt::QueuedConnection, which allows periodic processing of user events. Within this slot:

    • Check if the copy is not cancelled.
    • Read and write data in chunks using source and destination files.
    • Update progress based on the ratio of position to file size.
    • Recursively invoke the step() slot until the entire file is copied or cancelled.
  5. Define a cancel() slot to allow users to interrupt the copy operation and remove the destination file if necessary.
  6. Utilize signals to notify interested parties about progress changes and completion (done()).

Example Implementation

The following code snippet provides an example implementation of a copy helper class:

class CopyHelper : public QObject {
    Q_OBJECT
    Q_PROPERTY(qreal progress READ progress WRITE setProgress NOTIFY progressChanged)
public:
    CopyHelper(QString sPath, QString dPath, quint64 bSize = 1024 * 1024) :
        isCancelled(false), bufferSize(bSize), prog(0.0), source(sPath), destination(dPath), position(0) { }
    ~CopyHelper() { free(buff); }

    qreal progress() const { return prog; }
    void setProgress(qreal p) {
        if (p != prog) {
            prog = p;
            emit progressChanged();
        }
    }

public slots:
    void begin() {
        if (!source.open(QIODevice::ReadOnly)) {
            qDebug() << "could not open source, aborting";
            emit done();
            return;
        }
        fileSize = source.size();
        if (!destination.open(QIODevice::WriteOnly)) {
            qDebug() << "could not open destination, aborting";
            // maybe check for overwriting and ask to proceed
            emit done();
            return;
        }
        if (!destination.resize(fileSize)) {
            qDebug() << "could not resize, aborting";
            emit done();
            return;
        }
        buff = (char*)malloc(bufferSize);
        if (!buff) {
            qDebug() << "could not allocate buffer, aborting";
            emit done();
            return;
        }
        QMetaObject::invokeMethod(this, "step", Qt::QueuedConnection);
        //timer.start();
    }
    void step() {
        if (!isCancelled) {
            if (position < fileSize) {
                quint64 chunk = fileSize - position;
                quint64 l = chunk > bufferSize ? bufferSize : chunk;
                source.read(buff, l);
                destination.write(buff, l);
                position += l;
                source.seek(position);
                destination.seek(position);
                setProgress((qreal)position / fileSize);
                //std::this_thread::sleep_for(std::chrono::milliseconds(100)); // for testing
                QMetaObject::invokeMethod(this, "step", Qt::QueuedConnection);
            } else {
                //qDebug() << timer.elapsed();
                emit done();
                return;
            }
        } else {
            if (!destination.remove()) qDebug() << "delete failed";
            emit done();
        }
    }
    void cancel() { isCancelled = true; }

signals:
    void progressChanged();
    void done();

private:
    bool isCancelled;
    quint64 bufferSize;
    qreal prog;
    QFile source, destination;
    quint64 fileSize, position;
    char * buff;
    //QElapsedTimer timer;
};

Conclusion

By implementing a custom copy helper or using the provided example, it is possible to create a non-blocking file copy operation that can be interrupted by users. This approach allows for responsive and user-friendly file transfers, addressing the frustrations caused by lengthy operations that cannot be cancelled.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn