我有一个主线程和一个处理某些文件的线程。当主线程监视的文件夹发生更改时,会向处理线程发送信号以启动。处理完一个文件后,我想将其删除,然后让文件夹检查文件夹中是否还有其他文件。如果有,然后重复该过程。通过更改内容从文件夹中删除文件
我的问题是在文件夹的重复检查。在处理线程上。该功能在下面的代码中列出,问题是我无法从文件夹中删除文件。我相当卡住,所以任何输入赞赏。
在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
}
请让我知道如果我错过了提供一些信息。
您的后台打印程序服务是否正在运行? – Nejat
“我无法从文件夹中删除文件” - 这是什么意思? remove()是否返回false?然后使用'activeFile = file.absoluteFilePath();'来获得完整的路径,这更容易处理。然后,删除它:'QFile f(activeFile); if(!f.remove())qDebug(“Could not remove%s:%s”,qPrintable(activeFile),qPrintable(f.errorString()));'要了解为什么失败。 –
@KubaOber我初始化QString的原因是因为我在线程中初始化时遇到了其他问题。如果我例如定义一个int并且我没有用例如一个0,然后我得到什么看起来是一个内存地址,即使我没有初始化它作为一个指针。我有一天学会了如何使用线程,所以有些事情我还不确定。用'isEmpty()'表示好点。谢谢。 – Attaque