emit qt signal is very slow how it can be optimized?
I have a such peace of code.
thread that reeds the file
Code:
#include <QThread>
#include <QString>
{
Q_OBJECT
public:
~openner();
void run();
private:
signals:
void onStart(int);
void on(int);
void onEnd(int);
};
Code:
#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()
{
}
main form
Code:
#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);
};
Code:
#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()
{
}
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?
Re: emit qt signal is very slow how it can be optimized?
What happens if u use Qt::DirectConnection as last argument with connect ?
Re: emit qt signal is very slow how it can be optimized?
it's bad idea to use Qt:: DirectConnection between threads, Qt::QueuedConnection must be used.
Re: emit qt signal is very slow how it can be optimized?
I tried Qt::QueuedConnection and nothing changed.
Re: emit qt signal is very slow how it can be optimized?
yes, because by default last parameter in 'connect' is Qt::AutoConnection, which means
Quote:
If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt:: DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.
try to use customs events, but I think result will not change.
ps. if you use thread just for loading information and don't transform it then you can you only main thread and use qApp->processEvents() in long time operations.
Re: emit qt signal is very slow how it can be optimized?
a) Qt::QueuedConnection is the default for threads. So explicitly specifying it won't change anything.
b) simple solutions might be
- emit the signal not every time but only every 100th time (or so)
- just open the file in a normal function call, update the gui and call QCoreApplication::processEvents() now and then
- do not use a thread
- use a plain callback (do not forget to synchronize access! and don't forget you must not call gui code from another thread!)
HTH
Re: emit qt signal is very slow how it can be optimized?
What happen when You don't emit signal but You call in.pos() ?
Re: emit qt signal is very slow how it can be optimized?
Code:
#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()
{
}
Try the processEvents() function. I think that u emit too much signals, so the GUI its not able to process them during an intensive while loop.
Re: emit qt signal is very slow how it can be optimized?
OPPS! Sorry for the mistake
use QCoreApplication::processEvents();