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:
Implement a step() slot, utilizing QMetaObject::invokeMethod() with Qt::QueuedConnection, which allows periodic processing of user events. Within this slot:
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.
Das obige ist der detaillierte Inhalt vonWie kann ich Dateikopier- und Umbenennungsvorgänge in Qt unterbrechen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!