gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:56:38 28 4
gpt4 key购买 nike

周末我试图解决这个问题,但无济于事。我似乎找不到使用 QFileSystemWatcher::Files() 的示例直接所以我想我会问。

我有一个程序:

  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()));

插槽:

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。

我用过的一些相关链接:

QFileSystemWatcher working only in main()

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

最佳答案

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

QFileSystemWatcher examines each path added to it. Files that have been added to the QFileSystemWatcher can be accessed using the files() function, and directories using the directories() function.

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

关于c++ - 使用 QFileSystemWatcher::Files() 获取监视文件夹示例的文件数?计数总是0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40964800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com