2014-03-31 98 views
4

我有一个主线程和一个处理某些文件的线程。当主线程监视的文件夹发生更改时,会向处理线程发送信号以启动。处理完一个文件后,我想将其删除,然后让文件夹检查文件夹中是否还有其他文件。如果有,然后重复该过程。通过更改内容从文件夹中删除文件

我的问题是在文件夹的重复检查。在处理线程上。该功能在下面的代码中列出,问题是我无法从文件夹中删除文件。我相当卡住,所以任何输入赞赏。

在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 
} 

请让我知道如果我错过了提供一些信息。

+0

您的后台打印程序服务是否正在运行? – Nejat

+0

“我无法从文件夹中删除文件” - 这是什么意思? remove()是否返回false?然后使用'activeFile = file.absoluteFilePath();'来获得完整的路径,这更容易处理。然后,删除它:'QFile f(activeFile); if(!f.remove())qDebug(“Could not remove%s:%s”,qPrintable(activeFile),qPrintable(f.errorString()));'要了解为什么失败。 –

+0

@KubaOber我初始化QString的原因是因为我在线程中初始化时遇到了其他问题。如果我例如定义一个int并且我没有用例如一个0,然后我得到什么看起来是一个内存地址,即使我没有初始化它作为一个指针。我有一天学会了如何使用线程,所以有些事情我还不确定。用'isEmpty()'表示好点。谢谢。 – Attaque

回答

0

问题原来是两个问题。其中一个问题是系统速度太快,因此在系统同时读取和删除文件。我通过添加一个QTimer解决了这个问题,每当来自监控文件夹的信号被触发时,QTimer就被重置。距离系统的最后一次更改仅500毫秒就足够了。而不是继续使用QFileSystemWatcher读取该文件夹中的文件并将它们添加到队列中,正如我原来所做的那样,我创建了一个函数来在处理文件夹中的文件时将系统监视器静音。为了补偿文件监视器的功能,我在处理线程中创建了一个递归循环,以便只要文件仍然存在,它就会继续读取指定的文件。这可以在一个线程中完成。你们都聊代码,所以这里有云:

信号和插槽设置

// Setting up folder monitoring. 
watcher.addPath(worker->monitoredPath); 
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), this, 
SLOT(updateQueue())); 
// Timer 
connect(timer, SIGNAL(timeout()), this, SLOT(startProcess())); 
connect(this, SIGNAL(processRequest()), thread, SLOT(start())); 
... 

void MainWindow::updateQueue() { 
    // Starts or restarts the call to start process. Prevents multiple signals from 
    // many files added at once 
    timer->start(500); 
} 
... 
void MainWindow::startProcess() { 

    if(!thread->isRunning()) { 
    emit processRequest(); 
    muteWatcher(true); // From here on a recursive loop in dataprocessor checks 
         // the folder for new files. 
    } 
    timer->stop(); 

} 

静音文件守望

void MainWindow::muteWatcher(bool toggle) { 
    if(toggle) { 
    watcher.removePath(worker->monitoredPath); 
    } else { 
    watcher.addPath(worker->monitoredPath); 
    } 
} 

处理线程

void DataProcessor::initialize() { 
    QDir dir(this->monitoredPath); 
    dir.setFilter(QDir::NoDotAndDotDot | QDir::Files); 
    dir.setSorting(QDir::Time); 

    QList<QString> dFiles; 

    foreach(QFileInfo file, dir.entryInfoList()) { 

    if(file.suffix().toLower() == "txt") { 
     dFiles.append(file.fileName()); 
    } 
    } 

    if(dFiles.count() == 0) { // Base case 
    emit muteWatcher(false); // Start monitoring the folder again 
    emit finished(); // end the thread 
    return true; 
    } 

    // PROCESSING HERE 

    initializeLabel(); 

} 

接下来就是转到我打印的打印机的首选项,以防止此打印机将spool文件添加到该文件夹​​中。您可以通过点击“直接打印到打印机”来启用此功能。这解决了我的大部分问题,并且我能够删除我创建的一个延迟网络,以便使程序不会读取打印机生成的文件。

希望这可以帮助别人!

相关问题