如何使用QFileSystemWatcher监视文件夹以进行更改 [英] How to use QFileSystemWatcher to monitor a folder for change
问题描述
我是新的QT和我想使用QFileSystemWatcher来监视文件夹。
I'm new with QT and I want to use the QFileSystemWatcher to monitor a folder. I just can't figure how to do that.
我阅读了 http://qt-project.org/doc/qt-4.8/qfilesystemwatcher.html ,但我不知道如何初始化它。
I read http://qt-project.org/doc/qt-4.8/qfilesystemwatcher.html but I don't know how to even initialize it.
我没有找到一个例子,所以现在,如果有人可以发布一个解释或一个简单的例子来监视一个文件夹,没有更多。
I haven't found a single example, so now, I please if somebody could post an explanation or a simple example that monitors a folder and nothing more.
哦,这个应该是在控制台运行如果重要。
Oh, and this is supposed to run in console if it matters.
Thx为你的答案和问候。 p>
Thx for your answers and regards.
推荐答案
请看看这个.h和.cpp,它显示了例子... cheers!
Please have a look at this .h and .cpp , it shows the example... cheers !
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QWidget>
#include <QMessageBox>
class MyClass : public QWidget
{
Q_OBJECT
public:
MyClass(QWidget* parent=0)
:QWidget(parent){}
~MyClass(){}
public slots:
void showModified(const QString& str)
{
Q_UNUSED(str)
QMessageBox::information(this,"Directory Modified", "Your Directory is modified");
}
};
#endif // MYCLASS_H
#include <QApplication>
#include <QFileSystemWatcher>
#include <QDebug>
#include "MyClass.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QFileSystemWatcher watcher;
watcher.addPath("C:/QtTest");
QStringList directoryList = watcher.directories();
Q_FOREACH(QString directory, directoryList)
qDebug() << "Directory name" << directory <<"\n";
MyClass* mc = new MyClass;
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));
return app.exec();
}
无论何时修改,创建或删除C :/ QtTest路径,你会得到一个消息框。
When ever you modify, or create or delete a file or folder within "C:/QtTest" path you will get a message box.
这篇关于如何使用QFileSystemWatcher监视文件夹以进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!