这个周末我试着想办法解决这个问题,但没有结果。我似乎找不到一个直接使用QFileSystemWatcher::Files()的例子,所以我想我会问。
我有一个计划是:
我将声明我对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。
我使用过的一些相关链接:
http://doc.qt.io/qt-5/qfilesystemwatcher.html#files
发布于 2016-12-05 05:44:18
首先,让我们仔细看看docs (粗体格式是我的):
QFileSystemWatcher检查添加到它的每一条路径。添加到QFileSystemWatcherfiles()中的文件可以使用files()函数访问,目录可以使用directories()函数访问。
因此,files()只返回已经使用addPath()方法添加到观察者的文件列表,而不是通过添加目录隐式监视的文件列表。
发布于 2016-12-05 08:27:43
您可以通过使用QDir::entryInfoList和适用于您的过滤器来获取有关监视目录中文件的信息。至少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();
}https://stackoverflow.com/questions/40964800
复制相似问题