問(wèn)題描述
我是 QT 新手,我想使用 QFileSystemWatcher 來(lái)監(jiān)視文件夾.我就是不知道該怎么做.
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.
我還沒(méi)有找到一個(gè)例子,所以現(xiàn)在,如果有人可以發(fā)布一個(gè)解釋或一個(gè)簡(jiǎn)單的例子來(lái)監(jiān)視一個(gè)文件夾,僅此而已.
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.
哦,如果重要的話,這應(yīng)該在控制臺(tái)中運(yùn)行.
Oh, and this is supposed to run in console if it matters.
感謝您的回答和問(wèn)候.
推薦答案
請(qǐng)看一下這個(gè) .h 和 .cpp ,它顯示了示例...干杯!
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 <<"
";
MyClass* mc = new MyClass;
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));
return app.exec();
}
當(dāng)您在C:/QtTest"路徑中修改、創(chuàng)建或刪除文件或文件夾時(shí),您將收到一個(gè)消息框.
When ever you modify, or create or delete a file or folder within "C:/QtTest" path you will get a message box.
這篇關(guān)于如何使用 QFileSystemWatcher 監(jiān)視文件夾的更改的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!