2016-12-04 29 views
0

私は週末にこれを理解しようとしましたが、無駄です。 QFileSystemWatcher::Files()を使って例を見つけることはできないので、私は尋ねると思った。QFileSystemWatcher :: Files()を使用して監視フォルダのファイル数を取得する例?カウントは常に0ですか?

  1. は、ユーザーが「ソース」フォルダを選択することができます:

    は私がプログラムを持っています。

  2. ボタンを押して、新しいファイルのソースフォルダの視聴を開始します。
  3. ファイルが追加または削除されるたびにカウントを更新しようとすると、 '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

答えて

1

まず(太字フォーマットは私です)のは、ドキュメントを詳しく見てみましょう:

QFileSystemWatcherは、各パスを調べそれに加えられた。 QFileSystemWatcherに追加されたファイルは、files()ファンクションと、directories()ファンクションを使用するディレクトリを使用してアクセスできます。だから、

files()はあなたがすでにaddPath()方法、NOT暗黙的にディレクトリを追加することによって監視されているファイルのリストを使用してウォッチャーに追加したファイルのリストを返します。

1

ファイルの情報を監視対象ディレクトリに取得することができます。あなたのケースではとfiltersが適用されます。少なくとも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(); 
} 
関連する問題