Qt でファイルのコピーと名前変更の操作を中断するにはどうすればよいですか?

Susan Sarandon
リリース: 2024-11-13 15:33:02
オリジナル
994 人が閲覧しました

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 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート