使用QFileSystemWatcher :: Files()获取观察文件夹示例的文件数量?计数始终为0?

问题描述:

我试图弄清楚这个周末,但无济于事。我似乎找不到直接使用QFileSystemWatcher::Files()的例子,所以我想我会问。使用QFileSystemWatcher :: Files()获取观察文件夹示例的文件数量?计数始终为0?

我有一个程序是:

  1. 让用户选择一个“源”文件夹中。
  2. ,按下按钮,开始观看新文件
  3. 是源文件夹中有使用“directoryChanged()”所发出的信号,其中我想尽文件被添加或删除的时间来更新计数。

我会声称我的QfileSystemWatcher实现可能不正确。但是这个代码正在工作并确实触发信号/插槽。但数始终为零......

从mainwindow.cpp ...

信号:

//connect push buttons 
QObject::connect(ui->startButton, SIGNAL(clicked()), 
       this, SLOT(startButtonClicked())); 

//link qfilesystemwatcher with signals and slots 
    QObject::connect(&hotfolder, SIGNAL(directoryChanged(QString)), this, SLOT(hotfolderChanged())); 

插槽:

void MainWindow::startButtonClicked(){ 

    //start the file system watcher using the 'source folder button' 
    //first, get the resulting text from the source folder button 
    QString sourceText = ui->sourceBtnLineEdit->text(); 
    ui->statusbar->showMessage(sourceText); 

    //convert the text from source button to a standard string. 
    string filePath = sourceText.toStdString(); 
    cout << filePath << endl; 

    //call method to add source path to qfilesystemwatcher 
    startWatching(sourceText); 

} 

void MainWindow::hotfolderChanged(){ 

    int fileCount = filesWatched(); 
    ui->statusbar->showMessage(QString::number(fileCount)); 

} 
从magickWatcher.h

#ifndef MAGICKWATCHER_H 
#define MAGICKWATCHER_H 

#include <QFileSystemWatcher> 
#include <mainwindow.h> 

//create the qFileSystemWatcher 
QFileSystemWatcher hotfolder; 

//add folder to qfilesystemwatcher 
//starts watching of folder path 
int startWatching(QString folder){ 

    hotfolder.addPath(folder); 
    cout << "hotfolder created!" << endl; 
    return 0; 
} 

//get file list of folder being watched 
int filesWatched(){ 
    QStringList watchedList = hotfolder.files(); 

    //report out each line of file list 
    for (int i = 0; i < watchedList.size(); ++i){ 
      cout << watchedList.at(i).toStdString() << endl; 
      cout << "is this looping?!!" << endl; 
    } 
    return watchedList.count(); 
} 


#endif // MAGICKWATCHER_H 

我该如何使用QFil eSystemWatcher获取观看文件夹的文件数量?我知道QDir及其选项,但想知道如何使用QFileSystemWatcher。

我仍然围绕着C++一般,所以谢谢你的任何建议或提示以及。我想也许我的问题是我如何实现QFileSystemWatcher。

一些相关的链接我已经使用:

QFileSystemWatcher working only in main()

http://doc.qt.io/qt-5/qfilesystemwatcher.html#files

首先让我们在文档细看(粗体格式是我的):

QFileSystemWatcher检查每个路径添加到它。 可以使用files()函数和使用directories()函数的目录访问已添加到QFileSystemWatcher的文件。

所以,files()只返回您已使用addPath()方法,不会隐正在添加目录观看的文件列表已经加入到观察者的文件列表。

您可以获取有关被监视目录中文件的信息,例如通过使用QDir::entryInfoListfilters适用于您的情况。至少QDir::Files和可能的QDir::NoDotAndDotDot将是有意义的。

//get file list of folder being watched 
int filesWatched() { 
    QString folder = "/path/to/hotfolder/"; 
    QDir monitoredFolder(folder); 
    QFileInfoList watchedList = 
     monitoredFolder.entryInfoList(QDir::NoDotAndDotDot | QDir::Files); 

    QListIterator<QFileInfo> iterator(watchedList); 
    while (iterator.hasNext()) 
    { 
     QFileInfo file_info = iterator.next(); 
     qDebug() << "File path:" << file_info.absoluteFilePath(); 
    } 

    return watchedList.count(); 
}