[英]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.