#include "fileread.h"
#include "ui_fileread.h"
#include "filethread.h"
#include<QThread>
#include <QtGui>
#include<QLabel>
fileread
::fileread(QWidget *parent
) : ui(new Ui::fileread)
{
ui->setupUi(this);
}
fileread::~fileread()
{
delete ui;
}
void fileread::on_pushButton_clicked()
{
filethread fthread1(fname1),fthread2(fname2);
QObject::connect(&fthread1,
SIGNAL(setlabel
(QString)),ui
->label_2,
SLOT(setText
(QString)), Qt
::QueuedConnection);
QObject::connect(&fthread2,
SIGNAL(setlabel
(QString)),ui
->label_3 ,
SLOT(setText
(QString)), Qt
::QueuedConnection);
fthread1.start();
fthread2.start();
fthread1.wait();
fthread2.wait();
}
filethread.cpp
{
fname=text;
}
void filethread::run()
{
{
}
while (!line.isNull())
{
mutex.lock();
emit setlabel(line);
sleep(1);
qDebug()<<"Content from the file:"<<line;
line = in.readLine();
mutex.unlock();
}
}
#include "fileread.h"
#include "ui_fileread.h"
#include "filethread.h"
#include<QThread>
#include <QtGui>
#include<QLabel>
fileread::fileread(QWidget *parent) :
QWidget(parent),
ui(new Ui::fileread)
{
ui->setupUi(this);
}
fileread::~fileread()
{
delete ui;
}
void fileread::on_pushButton_clicked()
{
QString fname1="E:/QT/test1.txt";
QString fname2="E:/QT/test3.txt";
filethread fthread1(fname1),fthread2(fname2);
QObject::connect(&fthread1, SIGNAL(setlabel(QString)),ui->label_2, SLOT(setText(QString)), Qt::QueuedConnection);
QObject::connect(&fthread2, SIGNAL(setlabel(QString)),ui->label_3 , SLOT(setText(QString)), Qt::QueuedConnection);
fthread1.start();
fthread2.start();
fthread1.wait();
fthread2.wait();
}
filethread.cpp
filethread::filethread(QString text) : QThread()
{
fname=text;
}
QMutex mutex;
void filethread::run()
{
QFile fp(fname);
if(fp.open(QIODevice::ReadOnly))
{
QDataStream in(&fp);
}
QTextStream in(&fp);
QString line = in.readLine();
while (!line.isNull())
{
mutex.lock();
emit setlabel(line);
sleep(1);
qDebug()<<"Content from the file:"<<line;
line = in.readLine();
mutex.unlock();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks