我试图在周末解决这个问题,但无济于事。我似乎找不到直接使用QFileSystemWatcher::Files()的示例,所以我想我会问。
我有一个程序:
- 让用户选择一个“源”文件夹。
- 按下按钮开始查看该源文件夹中的新文件
- 使用'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
我如何使用 QFileSystemWatcher 来获取监视文件夹的文件数?我了解QDir及其选项,但想具体了解如何使用 QFileSystemWatcher。
总的来说,我仍然在围绕 C++ 进行思考,所以也感谢您提供的任何建议或提示。我想也许我的问题是我如何实现 QFileSystemWatcher。
我使用过的一些相关链接: