-2

I'm trying to create an application for learning purpose which reads a text file using threads, to exercise myself on working with threads in qt but i faced these errors enter image description here

My class name is Read and this is its Header file's code

#ifndef READ_H
#define READ_H

#include <fstream>
#include <QThread>

using namespace std;

#include <QObject>

class Read : public QThread
{
    Q_OBJECT
public:
    void reader();
    string text;

};

#endif // READ_H

And the source file's code is :

#include "read.h"

void Read::reader()
{
    ifstream x;
    x.open("/home/mohamed/test.txt");
    if(x.is_open())
    {
        while(!x.eof())
            getline(x,text);
    }
}

And i have one button and PlainTextEdit to view my text in it, And this is the code in butt pushButton_clicked event or slot

Read nread;
nread->start();

QString test = QString::fromStdString(nread->text);
ui->plainTextEdit->setPlainText(test);

Thats all, hope you can help me..thanks.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Moe
  • 432
  • 1
  • 6
  • 21
  • 4
    Possible duplicate of [Qt Linker Error: "undefined reference to vtable"](http://stackoverflow.com/questions/2555816/qt-linker-error-undefined-reference-to-vtable) – demonplus May 27 '16 at 05:38
  • 2
    Firstly, don't use images for text, it makes it hard to index and find similar issues. Then, try to do exactly that, search for the error message. You will find hundreds of similar questions here, make sure you understand what's going on in general and only ask another question if it is really different. – Ulrich Eckhardt May 27 '16 at 06:21

1 Answers1

3

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 &parameter) {
        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;
}
Shtol Krakov
  • 1,260
  • 9
  • 20