#ifndef READFILE_H
#define READFILE_H
#include <QtGui/QMainWindow>
#include "ui_readfile.h"
#include "DataFile.h"
#include <QThread>
class ReadFile : public QMainWindow
{
Q_OBJECT
public:
ReadFile(QWidget *parent = 0, Qt::WFlags flags = 0);
~ReadFile();
signals:
void signalRead();
private:
Ui::ReadFileClass ui;
DataFile* m_dataFile;
QThread* m_thread;
};
#endif // READFILE_H
#include "readfile.h"
#include <QDebug>
ReadFile::ReadFile(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
qDebug()<<"main Process"<<QThread::currentThreadId();
//1.按照顺序写2.信号槽(重点,这段代码非常重要,其余类的代码线程创建开启没有太大关系)
m_thread=new QThread(this);
m_dataFile=new DataFile();
connect(this,SIGNAL(signalRead()),m_dataFile,SLOT(readFile()));
m_dataFile->moveToThread(m_thread);
m_thread->start();
emit signalRead();
}
ReadFile::~ReadFile()
{
}
#ifndef DATAFILE_H
#define DATAFILE_H
#include <QObject>
#include <QThread>
class DataFile : public QObject
{
Q_OBJECT
public:
DataFile(QObject *parent=NULL);
~DataFile();
public slots:
void readFile();
private:
};
#endif // DATAFILE_H
#include "DataFile.h"
#include <QTextStream>
#include <QFile>
#include <QIODevice>
#include <QThread>
#include <QDebug>
DataFile::DataFile(QObject *parent)
: QObject(parent)
{
qDebug()<<"main process"<<QThread::currentThreadId();
}
DataFile::~DataFile()
{
}
void DataFile::readFile(){
qDebug()<<"Process 2"<<QThread::currentThreadId();
}