【问题标题】:How to use QFileSystemWatcher to monitor a folder for change如何使用 QFileSystemWatcher 监视文件夹的更改
【发布时间】:2012-04-06 14:25:08
【问题描述】:

我是 QT 新手,我想使用 QFileSystemWatcher 来监控文件夹。我只是想不出该怎么做。

我读过http://qt-project.org/doc/qt-4.8/qfilesystemwatcher.html,但我什至不知道如何初始化它。

我还没有找到一个示例,所以现在,如果有人可以发布一个解释或一个监视文件夹的简单示例,仅此而已。

哦,如果重要的话,这应该在控制台中运行。

感谢您的回答和问候。

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    请看一下这个 .h 和 .cpp ,它显示了示例...干杯!

    #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 <<"\n";
    
        MyClass* mc = new MyClass;
    
        QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));
    
        return app.exec();
    }
    

    当您在“C:/QtTest”路径中修改、创建或删除文件或文件夹时,您会看到一个消息框。

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多