我正在尝试使用 QFileSystemWatcher,如下例所示: 如何使用 QFileSystemWatcher 监视文件夹以进行更改
#include "mainwindow.h"
#include <QApplication>
#include <QFileSystemWatcher>
#include <QDebug>
#include "systemfilewatcher.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileSystemWatcher watcher;
watcher.addPath("C:/QtTest");
SystemFileWatcher* mc = new SystemFileWatcher();
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc,
SLOT(showModified(QString)));
MainWindow w(&watcher);
w.show();
return a.exec();
}
但是,当我在我的 ui 中尝试这个完全相同的代码时,如下所示,它不起作用:
MainWindow::MainWindow(QFileSystemWatcher folder, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemWatcher watcher;
watcher.addPath("C:/QtTest");
SystemFileWatcher* mc = new SystemFileWatcher();
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));
这是我的“systemfilewatcher.h”:
#ifndef SYSTEMFILEWATCHER
#define SYSTEMFILEWATCHER
#include <QWidget>
#include <QMessageBox>
class SystemFileWatcher : public QWidget
{
Q_OBJECT
public:
SystemFileWatcher(QWidget* parent=0)
:QWidget(parent){}
~SystemFileWatcher(){}
public slots:
void showModified(const QString& str)
{
QMessageBox::information(this,"Directory Modified", str);
}
};
#endif // SYSTEMFILEWATCHER
}
我的目标是检测何时在目标目录中创建文件并将其名称放入 QString 堆栈中。我不知道我在这里做错了什么,有人可以帮助我吗?