Hi i am implementing a simple threaded GUI application in QT 4.6.2. I am using QThread without subclassing it. I have made a call to the usleep() function in my start() function this however results in freezing of the GUI. How do i get around this. Below is the code
#ifndef ECGREADER_H
#define ECGREADER_H
#include<QObject>
class ecgreader : public QObject
{
Q_OBJECT
public:
ecgreader(QObject *parent=0);
~ecgreader();
public Q_SLOTS:
void start();
Q_SIGNALS:
void finished();
};
#endif // ECGREADER_H
Below is the start() function
void ecgreader::start()
{
int i= system("ls>output.txt");
SLEEP(10000);
if(i==0)
{
emit finished();
}
}
finally the call to start is made here
void Application::onbtnclicked()
{
QThread* thread=new QThread;
ecgreader* reader=new ecgreader;
reader->moveToThread(thread);
connect(thread,SIGNAL(started()),reader,SLOT(start()));
connect(reader,SIGNAL(finished()),thread,SLOT(quit()));
connect(reader,SIGNAL(finished()),reader,SLOT(deleteLater()));
connect(reader,SIGNAL(finished()),thread,SLOT(deleteLater()));
reader->start();
}
Please help
You have two problems: first you created the thread, but you never started it. Second you are directly calling start() on your reader instead of emiting a signal.
I think what you meant was to call thread->start() instead of reader->start():
void Application::onbtnclicked()
{
QThread* thread=new QThread;
ecgreader* reader=new ecgreader;
reader->moveToThread(thread);
connect(thread,SIGNAL(started()),reader,SLOT(start()));
connect(reader,SIGNAL(finished()),thread,SLOT(quit()));
connect(reader,SIGNAL(finished()),reader,SLOT(deleteLater()));
connect(reader,SIGNAL(finished()),thread,SLOT(deleteLater()));
thread->start();
}