需求:处理多个*.dat文件,为每一个*.dat开辟一个线程。
下面的代码是乞丐丁哥纯手工打造。再次强烈感谢天下热心人。
乞丐丁 的搞法是直接使用run()来运行work thread
还有个技巧就是通过构造函数来传递参数,这些参数在thread类里面是全局变量。
#ifndef TOOLREPLACE_H
#define TOOLREPLACE_H
#include <QtWidgets>
class CToolReplace : public QWidget
{
Q_OBJECT
public:
CToolReplace(QWidget *parent = 0);
~CToolReplace();
QStringList gasFileList;
signals:
void SigSendFileName(qint32, QString);
private slots:
void OpenFile();
void SendFiles();
void UpdateStatus(qint32 iRow, QString sStatus);
private:
void InitUI();
private:
QTableWidget *tbFile;
QMenuBar *mbMain;
QMenu *m_File;
QAction *a_Open;
QAction *a_Close;
QMenu *m_Operate;
QAction *a_Replace;
QToolBar *tbMain;
};
#endif // TOOLREPLACE_H
#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QThread>
class CWorkThread : public QThread
{
Q_OBJECT
public:
CWorkThread(qint32 iRow, QString sFileName, QObject *parent = 0);
void run();
signals:
void SigStatus(qint32, QString);
private:
qint32 m_Row;
QString m_FileName;
};
#endif // WORKTHREAD_H
#include "toolreplace.h"
#include "workthread.h"
CToolReplace::CToolReplace(QWidget *parent)
: QWidget(parent)
{
resize(710,500);
InitUI();
a_Replace->setEnabled(false);
}
CToolReplace::~CToolReplace()
{
}
void CToolReplace::InitUI()
{
mbMain = new QMenuBar(this);
m_File = mbMain->addMenu(tr("File"));
a_Open = m_File->addAction(tr("Open File"));
a_Open->setIcon(QIcon(":/CToolReplace/Open"));
m_File->addSeparator();
a_Close = m_File->addAction(tr("Close File"));
a_Close->setIcon(QIcon(":/CToolReplace/Close"));
m_Operate = mbMain->addMenu(tr("Operate"));
a_Replace = m_Operate->addAction(tr("Replace"));
a_Replace->setIcon(QIcon(":/CToolReplace/Replace"));
tbMain = new QToolBar(this);
tbMain->addAction(a_Open);
tbMain->addSeparator();
tbMain->addAction(a_Replace);
tbMain->addSeparator();
tbMain->addAction(a_Close);
tbFile = new QTableWidget;
tbFile->setColumnCount(2);
tbFile->setHorizontalHeaderLabels(QStringList()<<tr("File Name")<<tr("Status"));
tbFile->setColumnWidth(0,500);
tbFile->setColumnWidth(1,200);
QVBoxLayout *vl = new QVBoxLayout;
vl->setContentsMargins(0,0,0,0);
vl->setSpacing(0);
vl->addWidget(mbMain);
vl->addWidget(tbMain);
vl->addWidget(tbFile);
setLayout(vl);
connect(a_Open, SIGNAL(triggered()), this, SLOT(OpenFile()));
connect(a_Replace, SIGNAL(triggered()), this, SLOT(SendFiles()));
connect(a_Close, SIGNAL(triggered()), this, SLOT(close()));
}
void CToolReplace::OpenFile()
{
a_Replace->setEnabled(false);
while(tbFile->rowCount() != 0)
{
tbFile->removeRow(tbFile->rowCount());
}
gasFileList = QFileDialog::getOpenFileNames(this,tr("Select Files"),"D:/","Dat(*.dat)");
if(!gasFileList.isEmpty())
{
a_Replace->setEnabled(true);
}
for(qint32 i = 0; i < gasFileList.count(); i++)
{
tbFile->insertRow(i);
tbFile->setItem(i,0,new QTableWidgetItem(gasFileList.at(i)));
tbFile->setItem(i,1,new QTableWidgetItem(tr("Wait")));
}
}
void CToolReplace::SendFiles()
{
for(qint32 i = 0; i < gasFileList.count(); i++)
{
CWorkThread *myThread = new CWorkThread(i, gasFileList.at(i));
connect(myThread, SIGNAL(SigStatus(qint32, QString)), this, SLOT(UpdateStatus(qint32, QString)));
connect(myThread, SIGNAL(finished()), myThread, SLOT(deleteLater()));
myThread->start();
}
}
void CToolReplace::UpdateStatus(qint32 iRow, QString sStatus)
{
tbFile->setItem(iRow, 1, new QTableWidgetItem(sStatus));
}
#include "workthread.h"
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QDebug>
CWorkThread::CWorkThread(qint32 iRow, QString sFileName, QObject *parent)
: QThread(parent)
{
m_Row = iRow;
m_FileName = sFileName;
}
void CWorkThread::run()
{
QStringList asAllRow;
asAllRow.clear();
QFile m_File(m_FileName);
if(!m_File.open(QIODevice::ReadOnly | QIODevice::Text))
{
emit SigStatus(m_Row, QString(tr("Cannot open file for read: %1").arg(m_File.errorString())));
return;
}
emit SigStatus(m_Row, tr("1, Reading file~~~~~"));
QTextStream m_TextStream(&m_File);
m_TextStream.setCodec("UTF-8");
while(!m_TextStream.atEnd())
{
asAllRow.append(m_TextStream.readLine());
}
m_File.close();
qDebug()<<"str count:"<<asAllRow.count();
if (asAllRow.size() < 2)
{
qDebug()<<"Less than 2 row!";
return;
}
emit SigStatus(m_Row, tr(("2, Replacing file~~~")));
asAllRow.replace(1, asAllRow.at(2));
QFile m_FileWrite(m_FileName);
if(!m_FileWrite.open(QIODevice::WriteOnly | QIODevice::Text))
{
emit SigStatus(m_Row, QString(tr("Cannot open file for read: %1").arg(m_FileWrite.errorString())));
return;
}
emit SigStatus(m_Row, tr("3, Writing file~~~~~"));
QTextStream m_TextStreamOut(&m_FileWrite);
m_TextStreamOut.setCodec("UTF-8");
for(qint32 iOut = 0; iOut < asAllRow.count(); iOut++)
{
m_TextStreamOut<<asAllRow.at(iOut)<<"\n";
}
m_TextStream.flush();
m_FileWrite.close();
emit SigStatus(m_Row, tr("4, Finished!~~~~~~~~"));
}