简体   繁体   English

用一个线程读取,将数据发送到另一个线程

[英]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.在下面的代码中,我从 txt 读取数据并在我的线程上运行它。 I want to use another thread for writing the datas another txt file.我想使用另一个线程将数据写入另一个 txt 文件。 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.您应该继承QObject然后使用这个子类 object 在另一个线程中运行您的程序。 Use Qt's Qt::QueuedConnection (it's the default if the connection is between two threads) to communicate between two threads.使用Qt's Qt::QueuedConnection (如果连接在两个线程之间,则为默认值)在两个线程之间进行通信。 And don't forget to use QMutex when accessing same data from two different threads.当从两个不同的线程访问相同的数据时,不要忘记使用QMutex

Your MyThread class might look like:您的MyThread class 可能如下所示:

mythread.h我的线程.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我的线程.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.在子类QMainWindow中,您需要创建MyThread的新 object,然后将其移动到新线程。 You can do it as follows:你可以这样做:

mainwindow.h主窗口.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主窗口.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.此后 function MyThread::writeData()将在新线程中运行。 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.主线程和其他线程之间的连接是默认的Qt::QueuedConnection ,因此您不必担心如何建立连接。 Just keep in mind to use QMutex if you are accessing data from a different thread.如果您从不同的线程访问数据,请记住使用QMutex You can add many more functions and variables to the MyThread class and use it as a normal QObject subclass.您可以向MyThread class 添加更多函数和变量,并将其用作普通的QObject子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MemoryStream有一个线程写入它和另一个读取 - MemoryStream have one thread write to it and another read 如果使用EventWaitHandel,从另一个线程读取变量时,从一个线程分配的变量是否“线程安全”? - Are variables assigned from one thread “thread-safe” when read from another thread if an EventWaitHandel is used? 在另一个线程中读取变量 - Read a variable in another thread 如何将消息(在 C 中)从一个线程发送到另一个线程? - How to send message (in C) from one thread to another? Java Android:将数据从一个thead发送到另一个线程 - Java Android :Send data from one thead to another thread 在一个线程上使用ppoll阻塞并在另一个线程上读取/写入相同的fds是否安全? - Is it safe to block with ppoll on one thread, and read/write to the same fds on another? 从一个线程读取变量并从另一个线程写入的正确方法? - Proper way to read variable from one thread and write from another? 获取在一个线程中与另一个线程一起创建的数据 - Getting data created in one thread with another thread 在另一个线程中使用一个线程中生成的数据 - Use data generated in one thread in another thread 将字符串发送到另一个 python 线程 - Send a string to another python thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM