I have a such peace of code.
thread that reeds the file
#include <QThread>
#include <QString>
{
Q_OBJECT
public:
~openner();
void run();
private:
signals:
void onStart(int);
void on(int);
void onEnd(int);
};
#include <QThread>
#include <QString>
class openner : public QThread
{
Q_OBJECT
public:
openner(QString);
~openner();
void run();
private:
QString file;
signals:
void onStart(int);
void on(int);
void onEnd(int);
};
To copy to clipboard, switch view to plain text mode
#include "openner.h"
#include <QFile>
#include <QTextStream>
{
file=f;
}
void openner::run()
{
emit onStart(f.size());
while(!in.atEnd())
{
str=in.readLine();
emit on(in.pos());
}
emit onEnd(f.size());
f.close();
}
openner::~openner()
{
}
#include "openner.h"
#include <QFile>
#include <QTextStream>
openner::openner(QString f)
: QThread(0)
{
file=f;
}
void openner::run()
{
QString str;
QFile f(file);
f.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&f);
emit onStart(f.size());
while(!in.atEnd())
{
str=in.readLine();
emit on(in.pos());
}
emit onEnd(f.size());
f.close();
}
openner::~openner()
{
}
To copy to clipboard, switch view to plain text mode
main form
#include <QtGui/QMainWindow>
#include <QProgressBar>
#include "openner.h"
{
Q_OBJECT
private:
openner *o;
public:
progressTest
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~progressTest();
protected slots:
void onStart(int);
void onProgres(int);
void onEnd(int);
};
#include <QtGui/QMainWindow>
#include <QProgressBar>
#include "openner.h"
class progressTest : public QMainWindow
{
Q_OBJECT
private:
openner *o;
QProgressBar *pb;
public:
progressTest(QWidget *parent = 0, Qt::WFlags flags = 0);
~progressTest();
protected slots:
void onStart(int);
void onProgres(int);
void onEnd(int);
};
To copy to clipboard, switch view to plain text mode
#include "progresstest.h"
progressTest
::progressTest(QWidget *parent, Qt
::WFlags flags
){
pb->resize(200,20);
o=new openner("C:\\projects\\umc\\UMC_invoice_.28532.00.00.100000_id_0793918_part_1.csv");
QObject::connect(o,
SIGNAL(onStart
(int)),
this,
SLOT(onStart
(int)));
QObject::connect(o,
SIGNAL(on
(int)),
this,
SLOT(onProgres
(int)));
QObject::connect(o,
SIGNAL(onEnd
(int)),
this,
SLOT(onEnd
(int)));
o->start();
}
void progressTest::onStart(int m)
{
pb->setMaximum(m);
}
void progressTest::onProgres(int l)
{
pb->setValue(l);
}
void progressTest::onEnd(int l)
{
pb->setValue(0);
}
progressTest::~progressTest()
{
}
#include "progresstest.h"
progressTest::progressTest(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
pb=new QProgressBar(this);
pb->resize(200,20);
o=new openner("C:\\projects\\umc\\UMC_invoice_.28532.00.00.100000_id_0793918_part_1.csv");
QObject::connect(o,SIGNAL(onStart(int)),this,SLOT(onStart(int)));
QObject::connect(o,SIGNAL(on(int)),this,SLOT(onProgres(int)));
QObject::connect(o,SIGNAL(onEnd(int)),this,SLOT(onEnd(int)));
o->start();
}
void progressTest::onStart(int m)
{
pb->setMaximum(m);
}
void progressTest::onProgres(int l)
{
pb->setValue(l);
}
void progressTest::onEnd(int l)
{
pb->setValue(0);
}
progressTest::~progressTest()
{
}
To copy to clipboard, switch view to plain text mode
When I use my program with emit on(in.pos()); then it takes for about three hours to read file. But then I use it without emit on(in.pos()); file is read in the minutes. the size of file is 3000 kb
How the emit of signal can be optimised?
OS - windows XP
IDE - Microsoft Visual Studio 2008?
Bookmarks