我有一个主线程和一个处理文件的线程。当主线程监控的文件夹发生变化时,将发送信号给处理线程开始工作。在处理完文件后,我希望将其删除,然后让文件夹检查是否还有更多文件。如果有,则重复该过程。
我的问题在于在处理线程中重复检查文件夹。下面的代码列出了该函数,问题在于我无法从文件夹中删除文件。我陷入了困境,所以任何输入都会受到赞赏。
在dataprocessor.h中:
我的问题在于在处理线程中重复检查文件夹。下面的代码列出了该函数,问题在于我无法从文件夹中删除文件。我陷入了困境,所以任何输入都会受到赞赏。
在dataprocessor.h中:
...
QList<QString> justProcessed;
...
在dataprocessor.cpp文件中
void DataProcessor::onSignal() {
// Will keep running as long as there are files in the spool folder, eating it's way through
bool stillFiles = true;
QDir dir(this->monitoredPath);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::Time);
while(stillFiles) {
// Have to update on each iteration, since the folder changes.
dir.refresh();
QFileInfoList fileList = dir.entryInfoList();
QString activeFile = "";
foreach(QFileInfo file, fileList) {
if((file.suffix() == "txt") && !justProcessed.contains(file.fileName())) {
// Is a text file. Set for processing and break foreach loop
activeFile = file.fileName();
break;
}
}
// If none of the files passed the requirements, then there are no more spl files in the folder.
qDebug() << activeFile;
if(activeFile == "") {
qDebug() << "Finished";
emit finished();
stillFiles = false;
}
// File is a new file, start processing
qDebug() << "Selected for processing";
qDebug() << monitoredPath + "/" + activeFile;
if(!dir.remove(monitoredPath + "/" + activeFile)) qDebug() << "Could not remove file";
justProcessed.append(activeFile);
} // While end
}
如果我漏提供了一些信息,请告诉我。