首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用QFileSystemWatcher::Files()获取监视文件夹示例的文件计数?计数总是0?

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

Stack Overflow用户
提问于 2016-12-04 23:07:35
回答 2查看 992关注 0票数 0

这个周末我试着想办法解决这个问题,但没有结果。我似乎找不到一个直接使用QFileSystemWatcher::Files()的例子,所以我想我会问。

我有一个计划是:

  1. 让用户选择一个“源”文件夹。
  2. 按下按钮开始查看新文件的源文件夹。
  3. 有一个使用'directoryChanged()‘发出的信号,我试图在每次添加或删除文件时更新计数。

我将声明我对QfileSystemWatcher的实现可能不正确。但是这段代码正在工作,并且确实触发了信号/插槽。但计数总是零..。

从mainwindow.cpp..。

信号:

代码语言:javascript
复制
//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()));

插槽:

代码语言:javascript
复制
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

代码语言:javascript
复制
#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只在main()中工作

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

EN

回答 2

Stack Overflow用户

发布于 2016-12-05 05:44:18

首先,让我们仔细看看docs (粗体格式是我的):

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

因此,files()只返回已经使用addPath()方法添加到观察者的文件列表,而不是通过添加目录隐式监视的文件列表。

票数 1
EN

Stack Overflow用户

发布于 2016-12-05 08:27:43

您可以通过使用QDir::entryInfoList和适用于您的过滤器来获取有关监视目录中文件的信息。至少QDir::Files和可能的QDir::NoDotAndDotDot是有意义的。

代码语言:javascript
复制
//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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40964800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档