QFileSystemWatcher的使用
程序
.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QFileSystemWatcher>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void directroyChanged(QString path);
private:
QLabel *pathLabel;
QFileSystemWatcher fsWatcher;
};
#endif // WIDGET_H
.cpp
#include "widget.h"
#include <QVBoxLayout>
#include <QDir>
#include <QMessageBox>
#include <QApplication>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QStringList args=qApp->arguments();
QString path;
if(args.count()>1)
{
path=args[1];
}
else
{
path=QDir::currentPath();
}
pathLabel=new QLabel;
pathLabel->setText("监视的目录"+path);
QVBoxLayout*mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(pathLabel);
fsWatcher.addPath(path);
connect(&fsWatcher,SIGNAL(directoryChanged(QString)),this,SLOT(directroyChanged(QString)));
}
Widget::~Widget()
{
}
void Widget::directroyChanged(QString path)
{
QMessageBox::information(NULL,"目录发生变化",path);
}
效果展示