简体   繁体   中英

Read with one thread, send datas to another thread

Im working with threads. In the following code, i read datas from txt and run it on my thread. I want to use another thread for writing the datas another txt file. How can i do it?

#include "mythread.h"
#include <QtCore>
#include <QDebug>
#include <QFile>
MyThread::MyThread()
{

}

void MyThread::run()  //Reading file from txt with thread1
{
    QFile file("C:/Users/ilknu/Documents/untitled1/deneme.txt");

    if (file.open(QIODevice::ReadOnly))
    {
        QTextStream in (&file);
        while (!in.atEnd())
        {

            QString line = in.readLine();
            QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
            for(const QString &entry : list)
            {
                double num = entry.toDouble();
                qDebug()<<num;
                queue.enqueue(num);

            } // for
        } // while
    } // if

    file.close();
}

You should subclass QObject then use this subclassed object to run your program in another thread. Use Qt's Qt::QueuedConnection (it's the default if the connection is between two threads) to communicate between two threads. And don't forget to use QMutex when accessing same data from two different threads.

Your MyThread class might look like:

mythread.h

#include <QObject>
#include <QMutex>

class MyThread : public QObject
{
    Q_OBJECT

public:
    MyThread(QObject* parent = nullptr);
    ~MyThread();

signals:
    void writingDone(); // This signal is for main thread

public slots:
    void writeData(); // Slot to ask the thread to write data
};

mythread.cpp

#include "mythread.h"
MyThread::MyThread(QObject* parent)
    : QObject(parent)
{
    // Initialize the variables and call the functions you need
}

MyThread::~MyThread()
{
}

void MyThread::writeData()
{
    // Use this function to write data to a file
    // This function will run in a different thread
    // emit the signal when/if required
    emit writingDone();
}

// Add other functions if needed

In the subclasses QMainWindow you need to create a new object of MyThread and then move it to a new thread. You can do it as follows:

mainwindow.h

#include <QtWidgets/QMainWindow>
#include <QThread>
#include "mythread.h"
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = Q_NULLPTR);

private:
    Ui::MainWindowClass UI;
    MyThread* myThreadObject; // Create a object of MyThread
    QThread* myQThread; // Create an object of QThread

signals:
    void startWriting();

public slots:
    void writingDoneByThread();
};

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    // Initialize the new objects
    myThreadObject = new MyThread();
    myQThread = new QThread();

    // Move to new thread
    myThreadObject->moveToThread(myQThread);

    // connect signal and slots
    connect(this, &MainWindow::startWriting, myThreadObject, &MyThread::writeData);
    connect(myThreadObject, &MyThread::writingDone, this, &MainWindow::writingDoneByThread);

    // Start the new thread
    myQThread->start();
}

After this the function MyThread::writeData() will run in a new thread. The connect between the main thread and the other thread is the default Qt::QueuedConnection and so you don't have to worry about how the connection is made. Just keep in mind to use QMutex if you are accessing data from a different thread. You can add many more functions and variables to the MyThread class and use it as a normal QObject subclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM