在qt中创建线程时出错

时间:2016-05-27 05:01:12

标签: c++ linux multithreading qt

我正在尝试创建一个用于学习目的的应用程序,该应用程序使用线程读取文本文件,以便在qt中处理线程但我遇到了这些错误 enter image description here

我的类名是Read,这是它的Header文件的代码

#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

源文件的代码是:

#include "read.h"

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

我有一个按钮PlainTextEdit可以在其中查看我的文字,这是对接pushButton_clicked事件或广告位中的代码

Read nread;
nread->start();

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

多数民众赞成,希望你能帮助我......谢谢。

1 个答案:

答案 0 :(得分:3)

你错过了班上的构造函数:

class Read : public QThread
{
    Q_OBJECT
public:

    explicit Read(QObject *parent);
    ~Read();

    void reader();
    string text;

};

无论如何,你为什么需要继承QThread?您可以为目标使用完全信号/插槽方式。这是一个可以在official doc中使用的简单示例:

这是您未来的读文件类:

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);
};

这是控制器(主要类):

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:

目标的一个简单示例。 请记住它是写在膝盖上的。

读者.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();
};

读者.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();
}

主要类.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:
};

主要类.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;
}