PDA

View Full Version : The threaded signal/slot mechanism



xbtl
29th March 2008, 22:40
Hello everyone

I've encountered a problem while trying to implement a polling function to fetch data from a serial port.
Qt Version is 4.3.4 and I'm using QDevelop as IDE. When I hit compile, I get a debug and a release version.
At first, I tried to set up a timer and connect its signal to the polling-method. This did work, but only in the release version. It doesn't work in the debug version nor in a static version. This seems to indicate that I created some kind of half-working solution which I just hate.
So I started reading and searching for this problem on the internet and found that setting up a timer only works when it is inside a eventloop. Since my polling class is connected to the mainWindow-class via pointer, I figured this could be the problem (Is it?).
Therefore I created a QThread-inheritance(TimerThread) to contain the polling timer. This worked eventually, but still only in the release-version of my project.
Hmpf. There still seems to be something about these signals/slots I just don't get.

My goal is having a class that handles one serial port via qextserialport and fetches data from it on a fixed timetick completely on its own. Occasionally, it gets called and returns a chunk of data. And it should be able to be included everywhere by setting up a pointer with the type of this class and construct an object on it.

This is my implementation of the thread-class whick inherits QThread:


#include "TimerThread.h"
//************************************************** ****************************
TimerThread::TimerThread(SerialPort *port)
{
this->port=port;
}
//************************************************** ****************************
void TimerThread::run()
{
timer=new QTimer(this);
timer->start(30);
connect(timer, SIGNAL(timeout()), port, SLOT(fetchData()));
exec();
}
//************************************************** ****************************


So, the object of TimerThread knows the object it has been created by and uses this knowledge to connect the timer-signal to the method of its creator that collects data from the serial port.

This class-object gets created like that:



timerPort = new TimerThread(this);


Would really appreciate any help because I totally ran out of ideas.

xbtl
30th March 2008, 00:07
Solved it after reading
http://www.qtcentre.org/forum/f-qt-programming-2/t-qthread-multi-threaded-signals-and-slots-12721.html


I changed my TimerThread to have its own pseudo-polling method. This method can get connected just fine and does nothing except calling the actual polling-function.



#include "TimerThread.h"
//************************************************** ****************************
TimerThread::TimerThread(SerialPort *port)
{
this->port=port;
}
//************************************************** ****************************
void TimerThread::run()
{
timer=new QTimer(this);
timer->start(300);
connect(timer, SIGNAL(timeout()), this, SLOT(platform()));
exec();
}
//************************************************** ****************************
void TimerThread::platform()
{
port->fetchData();
}
//************************************************** ****************************


Ok then... Now my question is: Why did it work with the release-version?