Skip to content

Commit

Permalink
QFileSystemWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cppcloud committed Jun 30, 2024
1 parent 66cd07a commit e958cde
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 6-2-QFileSystemWatcher/6-2-QFileSystemWatcher.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
watcher.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
watcher.h
11 changes: 11 additions & 0 deletions 6-2-QFileSystemWatcher/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <QCoreApplication>
#include "watcher.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Watcher w;

return a.exec();
}
32 changes: 32 additions & 0 deletions 6-2-QFileSystemWatcher/watcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "watcher.h"

Watcher::Watcher(QObject *parent) : QObject(parent)
{
fsw.addPath(QDir::currentPath());
fsw.addPath(QDir::currentPath() + QDir::separator() + "test.txt");

connect(&fsw,&QFileSystemWatcher::fileChanged, this, &Watcher::fileChanged);
connect(&fsw,&QFileSystemWatcher::directoryChanged, this, &Watcher::directoryChanged);
}

void Watcher::fileChanged(const QString &path)
{
qInfo() << "File changed: " << path;
if(fsw.files().length()) {
qInfo() << "Files that have changed:";
foreach(QString file, fsw.files()) {
qInfo() << file;
}
}
}

void Watcher::directoryChanged(const QString &path)
{
qInfo() << "Directory changed: " << path;
if(fsw.directories().length()) {
qInfo() << "Dir changed:";
foreach(QString dir, fsw.directories()) {
qInfo() << dir;
}
}
}
25 changes: 25 additions & 0 deletions 6-2-QFileSystemWatcher/watcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef WATCHER_H
#define WATCHER_H

#include <QObject>
#include <QDebug>
#include <QDir>
#include <QFileSystemWatcher>

class Watcher : public QObject
{
Q_OBJECT
public:
explicit Watcher(QObject *parent = nullptr);

signals:

public slots:
void fileChanged(const QString &path);
void directoryChanged(const QString &path);

private:
QFileSystemWatcher fsw;
};

#endif // WATCHER_H

0 comments on commit e958cde

Please sign in to comment.