参考:http://blog.csdn.net/u010650281/article/details/51516776
qt本身提供了许多多线程的类,方便我们进行多线程的开发
创建线程,Qt 包含下面一些线程相关的类:
QThread 提供了开始一个新线程的方法
QThreadStorage 提供逐线程数据存储
QMutex 提供相互排斥的锁,或互斥量
QMutexLocker 是一个便利类,它可以自动对QMutex加锁与解锁
QReadWriterLock 提供了一个可以同时读操作的锁
QReadLocker与QWriteLocker 是便利类,它自动对QReadWriteLock加锁与解锁
QSemaphore 提供了一个整型信号量,是互斥量的泛化
QWaitCondition 提供了一种方法,使得线程可以在被另外线程唤醒之前一直休眠。
使用qt的多线程进行文件读写,我们只需要实现比较简单的方式。首先是实现一个线程类,
class threadreadfile :public QThread
{
Q_OBJECT
public:
signals:
void showfileText(double );
protected:
void run();
public:
QString filepath;
QString filedata;
double filesize;
double fileNum;
};
类只需要集成QThread然后实现run函数之后就可以调用 ,start函数开始多线程了,
thread.start(QThread::LowPriority);
thread.wait();
start函数里面的参数是定义这个线程的优先级,也方便在run函数里面识别线程。
界面与线程之间通讯
线程类是没有调用ui界面的接口的, 我们有时候在线程里面进行了操作需要改变界面的显示,比如progressbar就是已给典型的控件。通讯方式很简单:
1,在线程类里面定义一个信号量。
signals:
void showfileText(double );
2,在界面函数里面添加一个slot函数,
void processbar(double );
3,在run函数里面出发信号
emit showfileText(out.pos());
4,然后在界面函数里面添加一个connect
QObject::connect(&thread,SIGNAL(showfileText(double)),this,SLOT(processbar(double)));
thread是线程类的实例。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class threadreadfile :public QThread
{
Q_OBJECT
public:
signals:
void showfileText(double );
protected:
void run();
public:
QString filepath;
QString filedata[3];
double filesize;
double fileNum;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
threadreadfile thread,thread2;
public:
void newThread();
private slots:
void on_btnLoadFile_clicked();
void processbar(double );
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
thread.destroyed();
}
void MainWindow::newThread()
{
QObject::connect(&thread,SIGNAL(showfileText(double)),this,SLOT(processbar(double)));
thread.start(QThread::LowPriority);
thread2.start(QThread::HighPriority);
thread2.wait();
thread.wait();
}
void MainWindow::on_btnLoadFile_clicked()
{
QFileDialog openfile;
QString filenamepath = openfile.getOpenFileName(this,tr("get file path"),QDir::currentPath());
ui->lineEditFilePath->setText(filenamepath);
thread.filepath = filenamepath;
newThread();
if(thread.isFinished())
{
ui->textEdit->append(thread.filedata+thread2.filedata);
// ui->textEdit->append();
}
}
void MainWindow::processbar(double index)
{
qDebug()<<"processbar" <progressBar->value();
nowVaule = nowVaule + index*100/thread.filesize;
ui->progressBar->setValue(nowVaule);//
}
void threadreadfile::run()
{
Priority id = QThread::priority();
quint64 num=0;
if(id == QThread::LowPriority)
{
qDebug()<<"this is one";
}
if(id == QThread::HighPriority)
{
num = 1;
qDebug()<<"this is two";
}
QString filename = filepath;
QFile file(filename);
if (!file.open(QIODevice::ReadWrite )) //设置打开模式
return;
QTextStream out(&file); //设置一个文件流用于输出数据到文本
filesize = file.size();
qDebug()<
这段代码读取文件没有弄好,就是读取文件的时候,线程一会读的超界。我给定的是10306,但是最后游标(pos)显示的是10412,并不懂为啥read规定了大小只有,还会读的超界。
updata - 6.2
已经解决了上面的问题,就是新建两个类thread,thread2,进行读取,然后把文本的打开方式| QIODevice::Text去掉,因为一般的文件按照这个格式打开会有格式错乱,导致了读取数量不同。