You missed constructor in your class:
class Read : public QThread
{
Q_OBJECT
public:
explicit Read(QObject *parent);
~Read();
void reader();
string text;
};
Anyway, why do you need to inherit QThread
? You can use fully signal/slot way for your target. This is a simple example you can use from official doc:
This is your future read-file class:
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork(const QString ¶meter) {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady(const QString &result);
};
This is controller (main class):
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
public slots:
void handleResults(const QString &);
signals:
void operate(const QString &);
};
UPD:
A simple example for your target. Remember that it was written on a knee.
Reader .H:
#include <QObject>
#include <QString>
#include <QFile>
#include <QThread>
class Reader : public QObject
{
Q_OBJECT
QString _text;
QFile _x;
public:
signals:
void over();
public slots:
void startRead();
void stopRead();
};
Reader .CPP:
#include "reader.h"
#include <QDebug>
void Reader::startRead()
{
_x.setFileName("test.txt");
if(_x.open(QIODevice::ReadOnly))
while(!_x.atEnd()) {
thread()->sleep(1);
qDebug() << _x.readLine();
}
if(_x.isOpen())
_x.close();
emit over();
}
void Reader::stopRead()
{
if(_x.isOpen())
_x.close();
}
Mainclass .H:
#include <QMainWindow>
#include <QThread>
#include "reader.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
QThread _thread;
Ui::MainWindow *ui;
Reader *_rdr;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void startWork();
void stopWork();
public slots:
};
Mainclass .CPP:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Here you create your worker class
_rdr = new Reader();
// Here you put it to thread
_rdr->moveToThread(&_thread);
// Here you connect a signal from QThread, that if it starts
// your worker class will start its work
//connect(&_thread, &QThread::started, _rdr, &Reader::startRead);
// This is destruction signal to destroy your worker class after thread is over
connect(&_thread, &QThread::finished, _rdr, &Reader::deleteLater);
// This is a signal from your worker class that will emit when job would be done.
// After that, thread will successful exit
connect(_rdr, &Reader::over, &_thread, &QThread::quit);
connect(this, &MainWindow::startWork, _rdr, &Reader::startRead);
connect(this, &MainWindow::stopWork, _rdr, &Reader::stopRead, Qt::DirectConnection);
// Start the thread
_thread.start();
emit startWork();
}
MainWindow::~MainWindow()
{
emit stopWork();
_thread.quit();
_thread.wait();
delete ui;
}