QFileSystemWatcher使用整理

来源:互联网 发布:上海巨人网络校园招聘 编辑:程序博客网 时间:2024/05/22 05:08

1.效果图
这里写图片描述

2.源代码
watch.h

#ifndef WATCHER_H#define WATCHER_H#include <QWidget>#include <QLabel>#include <QFileSystemWatcher>class Watcher : public QWidget{    Q_OBJECTpublic:    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);}
原创粉丝点击