TorAn
7th March 2019, 20:35
This is the development based on my question here (https://qtcentre.org/threads/70057-Seeking-design-advise)
My problem now is that I can't get the signal directoryChanged of the class QFileSystemWatcher to work with the slot in the thread where QFileSystemWatcher is created. Following is the code:
Any help with getting it to work correctly (catch the signal) will be greatly appreciated.
Thanks!
class threadedScraper : public QThread
{
Q_OBJECT
int _id;
QMutex& _mtx;
QString _path;
QFileSystemWatcher* _filesystemwatcher;
void run() override
{
_filesystemwatcher = new QFileSystemWatcher();
_filesystemwatcher->moveToThread(this);
_filesystemwatcher->addPath(_path);
// I tried different connection types. Qt::ConnectionType::DirectConnection does not work either
if (! connect(_filesystemwatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(ondirectoryChanged(const QString&))))
{
qDebug() << "failure to connect to QFileSystemWatcher::directoryChanged signal";
return;
}
qDebug() << QString("cycle %1: waiting for file creation in %2").arg(_id).arg(_path);
exec();
qDebug() << QString("cycle %1: stopped waiting for file").arg(_id);
}
private slots:
void ondirectoryChanged (const QString& p)
{
qDebug() << "cycle:" << QString::number(_id) << "file:" <<p;
_mtx.unlock();
}
public:
threadedScraper(int id, QMutex& m, QString p) : _id(id), _mtx(m), _path(p), _filesystemwatcher(nullptr), QThread()
{
}
virtual ~threadedScraper()
{
if (_filesystemwatcher != nullptr)
delete _filesystemwatcher;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMutex mtx;
QString directoryToMonitor="c:/temp/files";
QWaitCondition wc;
QTimer::singleShot(2000,&a, [&](){
qDebug()<< "started scraping";
for (int i = 0; i < 2; ++i) {
qDebug() << "starting cycle " << QString::number(i);
threadedScraper worker (i, mtx, directoryToMonitor);
worker.start();
mtx.lock();
wc.wait(&mtx, 5000); // I need to wait until the file is found in the monitoring directory or 5 seconds elapsed
worker.exit(0); // stop the thread
worker.wait();
mtx.unlock();
}
qDebug()<< "finished scraping";
});
return a.exec();
}
My problem now is that I can't get the signal directoryChanged of the class QFileSystemWatcher to work with the slot in the thread where QFileSystemWatcher is created. Following is the code:
Any help with getting it to work correctly (catch the signal) will be greatly appreciated.
Thanks!
class threadedScraper : public QThread
{
Q_OBJECT
int _id;
QMutex& _mtx;
QString _path;
QFileSystemWatcher* _filesystemwatcher;
void run() override
{
_filesystemwatcher = new QFileSystemWatcher();
_filesystemwatcher->moveToThread(this);
_filesystemwatcher->addPath(_path);
// I tried different connection types. Qt::ConnectionType::DirectConnection does not work either
if (! connect(_filesystemwatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(ondirectoryChanged(const QString&))))
{
qDebug() << "failure to connect to QFileSystemWatcher::directoryChanged signal";
return;
}
qDebug() << QString("cycle %1: waiting for file creation in %2").arg(_id).arg(_path);
exec();
qDebug() << QString("cycle %1: stopped waiting for file").arg(_id);
}
private slots:
void ondirectoryChanged (const QString& p)
{
qDebug() << "cycle:" << QString::number(_id) << "file:" <<p;
_mtx.unlock();
}
public:
threadedScraper(int id, QMutex& m, QString p) : _id(id), _mtx(m), _path(p), _filesystemwatcher(nullptr), QThread()
{
}
virtual ~threadedScraper()
{
if (_filesystemwatcher != nullptr)
delete _filesystemwatcher;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMutex mtx;
QString directoryToMonitor="c:/temp/files";
QWaitCondition wc;
QTimer::singleShot(2000,&a, [&](){
qDebug()<< "started scraping";
for (int i = 0; i < 2; ++i) {
qDebug() << "starting cycle " << QString::number(i);
threadedScraper worker (i, mtx, directoryToMonitor);
worker.start();
mtx.lock();
wc.wait(&mtx, 5000); // I need to wait until the file is found in the monitoring directory or 5 seconds elapsed
worker.exit(0); // stop the thread
worker.wait();
mtx.unlock();
}
qDebug()<< "finished scraping";
});
return a.exec();
}