1.效果图
2.源代码
watch.h
#ifndef WATCHER_H
#define WATCHER_H
#include <QWidget>
#include <QLabel>
#include <QFileSystemWatcher>
class Watcher : public QWidget
{
Q_OBJECT
public:
Watcher(QWidget *parent = 0);
~Watcher();
public slots:
void directoryChanged(QString path);//目录发生变化时响应
private:
QLabel *pathLabel;
QFileSystemWatcher fsWatcher;
};
#endif // WATCHER_H
watch.cpp
#include "watcher.h"
#include <QVBoxLayout>
#include <QDir>
#include <QMessageBox>
#include <QApplication>
Watcher::Watcher(QWidget *parent)
: QWidget(parent)
{
QStringList args=qApp->arguments();
QString path = "D:/document";
// if(args.count()>1)
// {
// path=args[1];
// }
// else
// {
// path=QDir::currentPath();
// }
pathLabel = new QLabel;
pathLabel->setText(tr("监视的目录:")+path);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(pathLabel);
fsWatcher.addPath(path);
connect(&fsWatcher,SIGNAL(directoryChanged(QString)),this,SLOT(directoryChanged(QString)));
}
Watcher::~Watcher()
{
}
void Watcher::directoryChanged(QString path)
{
QMessageBox::information(NULL,tr("目录发生变化"),path);
}