I did it using the class QEventLoop. Would it be the recommended way to run this event loop?
This is what I did:
main.cpp:
#include <QCoreApplication>
#include <QThread>
#include <iostream>
#include "filewatcher.h"
int main (int argc, char *argv[])
{
FileWatcher *close = new FileWatcher ("c:/home/rs94036/");
QObject::connect (close,
SIGNAL (removed
()), close,
SLOT (removedSlot
()));
QObject::connect (close,
SIGNAL (modified
()), close,
SLOT (modifiedSlot
()));
QObject::connect (close,
SIGNAL (created
()), close,
SLOT (createdSlot
()));
QObject::connect (close,
SIGNAL (exitLoop
()),
&loop,
SLOT (quit
()));
close->closeSepApp ();
loop.exec ();
std::cout << "AFTER LOOP" << std::endl;
return 0;
}
#include <QCoreApplication>
#include <QThread>
#include <iostream>
#include "filewatcher.h"
int main (int argc, char *argv[])
{
QCoreApplication a (argc, argv);
QEventLoop loop;
FileWatcher *close = new FileWatcher ("c:/home/rs94036/");
QObject::connect (close, SIGNAL (removed ()), close, SLOT (removedSlot ()));
QObject::connect (close, SIGNAL (modified ()), close, SLOT (modifiedSlot ()));
QObject::connect (close, SIGNAL (created ()), close, SLOT (createdSlot ()));
QObject::connect (close, SIGNAL (exitLoop ()), &loop, SLOT (quit ()));
close->closeSepApp ();
loop.exec ();
std::cout << "AFTER LOOP" << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
FileWatcher.cpp:
#include <QFileInfo>
#include <QTextStream>
#include "FileWatcher.h"
#include <iostream>
FileWatcher
::FileWatcher (const QString &path
){
monitoringPath = path;
status = FILE_TIMEOUT;
}
bool FileWatcher::closeSepApp ()
{
monitoringFileName = "close.txt";
QFile *close
= new QFile (monitoringPath
+ monitoringFileName
);
if (!close->exists ())
{
{
// Write something on it
*text << "1";
close->close ();
}
else
return false;
}
else
return false;
startMonitoring ();
return true;
}
void FileWatcher::startMonitoring ()
{
existsBeforeEvent
= QFileInfo (monitoringPath
+ monitoringFileName
).
exists();
status = existsBeforeEvent ? FILE_CHANGED : FILE_TIMEOUT;
connect (mon,
SIGNAL (fileChanged
(QString)),
this,
SLOT (onFileChanged
(QString)));
mon->addPath (monitoringPath + monitoringFileName);
}
void FileWatcher::onFileChanged (const QString& path)
{
bool existsAfterEvent
= QFileInfo (path
).
exists();
if (existsBeforeEvent && !existsAfterEvent)
emit removed ();
else if (!existsBeforeEvent && existsAfterEvent)
emit created ();
else
emit modified ();
}
void FileWatcher::removedSlot ()
{
std::cout << "FILE_REMOVED" << std::endl;
emit exitLoop ();
}
void FileWatcher::modifiedSlot ()
{
std::cout << "FILE_CHANGED" << std::endl;
emit exitLoop ();
}
void FileWatcher::createdSlot ()
{
std::cout << "FILE_CREATED" << std::endl;
emit exitLoop ();
}
#include <QFileInfo>
#include <QTextStream>
#include "FileWatcher.h"
#include <iostream>
FileWatcher::FileWatcher (const QString &path)
{
monitoringPath = path;
status = FILE_TIMEOUT;
}
bool FileWatcher::closeSepApp ()
{
monitoringFileName = "close.txt";
QFile *close = new QFile (monitoringPath + monitoringFileName);
if (!close->exists ())
{
if (close->open (QIODevice::WriteOnly))
{
// Write something on it
QTextStream *text = new QTextStream (close);
*text << "1";
close->close ();
}
else
return false;
}
else
return false;
startMonitoring ();
return true;
}
void FileWatcher::startMonitoring ()
{
existsBeforeEvent = QFileInfo (monitoringPath + monitoringFileName).exists();
status = existsBeforeEvent ? FILE_CHANGED : FILE_TIMEOUT;
mon = new QFileSystemWatcher ();
connect (mon, SIGNAL (fileChanged (QString)), this, SLOT (onFileChanged (QString)));
mon->addPath (monitoringPath + monitoringFileName);
}
void FileWatcher::onFileChanged (const QString& path)
{
bool existsAfterEvent = QFileInfo (path).exists();
if (existsBeforeEvent && !existsAfterEvent)
emit removed ();
else if (!existsBeforeEvent && existsAfterEvent)
emit created ();
else
emit modified ();
}
void FileWatcher::removedSlot ()
{
std::cout << "FILE_REMOVED" << std::endl;
emit exitLoop ();
}
void FileWatcher::modifiedSlot ()
{
std::cout << "FILE_CHANGED" << std::endl;
emit exitLoop ();
}
void FileWatcher::createdSlot ()
{
std::cout << "FILE_CREATED" << std::endl;
emit exitLoop ();
}
To copy to clipboard, switch view to plain text mode
FileWatcher.h:
#ifndef FileWatcher_H
#define FileWatcher_H
#include <QFileSystemWatcher>
#include <QObject>
#include <QEventLoop>
enum FileStatus
{
FILE_CREATED,
FILE_CHANGED,
FILE_REMOVED,
FILE_TIMEOUT
};
{
Q_OBJECT
public:
FileWatcher (const QString& path);
bool closeSepApp ();
private:
bool existsBeforeEvent;
FileStatus status;
public slots:
void startMonitoring ();
protected slots:
void onFileChanged (const QString& path);
signals:
void removed ();
void modified ();
void created ();
void exitLoop ();
public slots:
void removedSlot ();
void modifiedSlot ();
void createdSlot ();
};
#endif // FileWatcher_H
#ifndef FileWatcher_H
#define FileWatcher_H
#include <QFileSystemWatcher>
#include <QObject>
#include <QEventLoop>
enum FileStatus
{
FILE_CREATED,
FILE_CHANGED,
FILE_REMOVED,
FILE_TIMEOUT
};
class FileWatcher : public QObject
{
Q_OBJECT
public:
FileWatcher (const QString& path);
bool closeSepApp ();
private:
QString monitoringPath;
QString monitoringFileName;
bool existsBeforeEvent;
FileStatus status;
QFileSystemWatcher *mon;
public slots:
void startMonitoring ();
protected slots:
void onFileChanged (const QString& path);
signals:
void removed ();
void modified ();
void created ();
void exitLoop ();
public slots:
void removedSlot ();
void modifiedSlot ();
void createdSlot ();
};
#endif // FileWatcher_H
To copy to clipboard, switch view to plain text mode
Renan
Bookmarks