私は週末にこれを理解しようとしましたが、無駄です。 QFileSystemWatcher::Files()を使って例を見つけることはできないので、私は尋ねると思った。QFileSystemWatcher :: Files()を使用して監視フォルダのファイル数を取得する例?カウントは常に0ですか?
- は、ユーザーが「ソース」フォルダを選択することができます:
は私がプログラムを持っています。
- ボタンを押して、新しいファイルのソースフォルダの視聴を開始します。
- ファイルが追加または削除されるたびにカウントを更新しようとすると、 '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()));
スロット:magickWatcher.h
から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));
}
#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