-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |