首页 > 后端开发 > C++ > 正文

如何中断 Qt 中的文件复制和重命名操作?

Susan Sarandon
发布: 2024-11-13 15:33:02
原创
931 人浏览过

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.

以上是如何中断 Qt 中的文件复制和重命名操作?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板