QT将另一个线程的数据写入txt文件



我想从另一个线程在txt文件中写入数据。文本文件将被命名为currentTime()并每分钟周期性地创建一次。所以,使用QTimer制作的Txt文件每分钟都很好,但在Txt文件中,内容为空。当然,这个结果是正确的。因为我没有从另一个线程连接数据。如何连接或从另一个线程发送数据到QTimer使文本文件每分钟?例如,另一个线程每秒钟获取数据,QTimer每分钟生成文本文件。理想情况下,另一个线程每分钟获得60个数据,每分钟生成一个文本文件。我想一次写入60个数据到txt文件。

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QTcpSocket>
#include <QThread>
#include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(qintptr ID, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
void sendValue(QString strValue);
void close(QString disconnectId);
private slots:
void readyRead();
void disconnected();
void InitThreadObjects();
void sendData();
private:
QTcpSocket *socket;
qintptr socketDescriptor;
QByteArray Data;
};
#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QThread>
#include <QTimer>
#include <QTime>
#include <QFile>
#include <QString>
#include <QTextStream>
MyThread::MyThread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
}
int count = 0;
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
//        This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(this, &MyThread::started, this, &MyThread::InitThreadObjects);
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << "Client connected";
QString strValue = QString::number(socketDescriptor);
emit sendValue(strValue);
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies

exec();
}
void MyThread::readyRead()
{
// get the information
Data = socket->readAll();
// will write on server side window
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
QString disconnectId = QString::number(socketDescriptor);
emit close(disconnectId);
socket->deleteLater();
exit(0);
}
void MyThread::InitThreadObjects(){
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(sendData()));
timer->start(60000);
}

void MyThread::sendData(){
QTime currentTime = QTime::currentTime();
QString time = currentTime.toString("hhmmss");
QString filename = "/home/pi/"+time+".txt";
qDebug() << filename;
QFile file(filename);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "Could not open file for writing";
return;
}
QTextStream out(&file);
out << Data;
file.flush();
file.close();
}

您需要在另一个线程中创建QTimer

public slots:
void InitThreadObjects();

和在构造函数中添加初始化

connect(this, &Thread::started, this, &Thread::InitThreadObjects);
void Thread::InitThreadObjects(){
m_Timer = new QTimer(this);
connect(m_Timer, &QTimer::timeout, this, &Thread::TimeWriteData);
m_Timer->start(60 * 1000);
}

记录收集到的数据

void Thread::TimeWriteData(){
// Write data to file
}