Hi everyone.
I'm working on a little desktop gui app, which starts some other thread which would watch for changes in a certain file. I know I don't need a second thread for this, but would really like to, since I would do some time consuming processing on that file change event and would like to keep my main thread where gui app resides, still responsive to user comands.
Ok, so I have my second thread, but the problem is, that I can not connect signal-slot in it. The message I get at run time is "QObject::connect: No such slot QThread::HandleFileChange(const QString)". The relevant part of the code is bellow. Much thanks for help.
worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QThread>
#include <QFileSystemWatcher>
{
public:
void run();
private:
int Cntr;
public slots:
void HandleFileChange
(const QString fileName
);
};
#endif // WORKER_H
#ifndef WORKER_H
#define WORKER_H
#include <QThread>
#include <QFileSystemWatcher>
class Worker: public QThread
{
public:
Worker(QString SrcFileParam,QString WorkerFolderParam);
void run();
private:
QString SrcFilename,WorkerFolder;
int Cntr;
QFileSystemWatcher * watcher;
public slots:
void HandleFileChange(const QString fileName);
};
#endif // WORKER_H
To copy to clipboard, switch view to plain text mode
worker.cpp
#include "Worker.h"
#include <iostream>
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>
{
SrcFilename = SrcFileParam;
WorkerFolder = WorkerFolderParam;
watcher->addPath(this->SrcFilename);
}
void Worker::run() {
connect(watcher,
SIGNAL(fileChanged
(QString)),
this,
SLOT(HandleFileChange
(const QString)));
//problematical line for (int i=1;i<100;i++) {
//std::cout << qPrintable(i);
//std::cout << "printed from Worker class \nl";
//QMessageBox::information(0,tr("Info"),QString::number(i));
}
}
void Worker
::HandleFileChange(const QString fileName
) { // some code
}
#include "Worker.h"
#include <iostream>
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>
Worker::Worker(QString SrcFileParam,QString WorkerFolderParam)
{
SrcFilename = SrcFileParam;
WorkerFolder = WorkerFolderParam;
watcher = new QFileSystemWatcher(this);
watcher->addPath(this->SrcFilename);
}
void Worker::run() {
connect(watcher,SIGNAL(fileChanged(QString)),this,SLOT(HandleFileChange(const QString))); //problematical line
for (int i=1;i<100;i++) {
//std::cout << qPrintable(i);
//std::cout << "printed from Worker class \nl";
//QMessageBox::information(0,tr("Info"),QString::number(i));
}
}
void Worker::HandleFileChange(const QString fileName) {
// some code
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
Worker wkr1(ui->lineEdit->text(),ui->lineEdit_2->text());
wkr1.start();
wkr1.wait();
Worker wkr1(ui->lineEdit->text(),ui->lineEdit_2->text());
wkr1.start();
wkr1.wait();
To copy to clipboard, switch view to plain text mode
Bookmarks