使用QFileSystemWatcher :: Files()获取监视文件夹示例的文件数?计数总是0?

时间:2016-12-04 23:07:35

标签: c++ qt cross-platform qfilesystemwatcher

我试图在周末解决这个问题,但无济于事。我似乎无法直接使用QFileSystemWatcher::Files()找到一个例子,所以我想我会问。

我有一个程序:

  1. 让用户选择“来源”文件夹。
  2. 按一个按钮开始观看新文件的源文件夹
  3. 使用'directoryChanged()'发出信号,每次添加或删除文件时我都会尝试更新计数。
  4. 我会断言我的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
    

    如何使用QFileSystemWatcher获取被监视文件夹的文件数?我知道QDir及其选项,但想具体了解如何使用QFileSystemWatcher。

    我仍然总体上围绕着c ++,所以感谢你提出任何建议或提示。我想也许我的问题是我如何实现QFileSystemWatcher。

    我使用的一些相关链接:

    QFileSystemWatcher working only in main()

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

1 个答案:

答案 0 :(得分:1)

您可以在监控目录中获取有关文件的信息,例如使用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();
}