如何使用 QFileSystemWatcher 监视文件夹的更改

How to use QFileSystemWatcher to monitor a folder for change(如何使用 QFileSystemWatcher 监视文件夹的更改)
本文介绍了如何使用 QFileSystemWatcher 监视文件夹的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是 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.

感谢您的回答和问候.

推荐答案

请看一下这个 .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();
}

当您在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 监视文件夹的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)