PDA

View Full Version : understanding threads / serial communication with thread



brott
7th March 2010, 15:43
Hello all,

this is my first post in this forum, so please forgive me if there is something strange or against your rules.
(You might also recognize that I am not a native speaker).

I'm trying to learn Qt and practiced some tutorials. My first target is to build a serial-bus communication. For that I studied the Classes 'QextSerialPort' and 'qserialdevice' and decided finaly to try my luck with the 'QextSerialPort' in the 'one-class'-configuration. Finaly I want to communicate this program with my microcontrollers. (in the first step similarly like a terminal).

In order to do this, I wanted to use a seperate thread to listen if there are any kind of data comming in. I had to leran, that it es useful to understand first how threads are working.
Therefore I programmed a little Widget with a second Thread. Now my problems:

When I start the thread from my widget, it starts successfully and works pretty fine. But I don't know how do quit this thread. In the Qt documentation the methods 'quit()' and 'exit(0)' are mentioned. But when I test this, the thread works on.
A second Problem is that I need to communicate with the Widget where my QextSerialPort-Class is implemented. Unfortunately a communication between Widgets and Threads is impossible (Qt Help). So what is the best way to do it? Does it make sense to do the whole Serial-Communication in the thread or is there a different and better way to work with a thread?

btw.: Qt 4.5 , linux (ubuntu 9.10), Qt Creator.

This is the code of my little test-thread:


void MyThread::run()
{
qDebug() << "In Thread";
exec();
}

void MyThread::testslot()
{
qDebug()<<"testslot opend";
}

void MyThread::killslot()
{
qDebug()<<"Kill thread";
exit(0);
}


Thanks for your Help
(if there is more code necessary, please let me know)

toutarrive
7th March 2010, 16:15
QThread:run() : Returning from this method will end the execution of the thread

toutarrive
7th March 2010, 16:44
You can communicate with a widget (or several widgets) and a thread by using signal and slot.
Just have a look at the Qt demo.

The only thing you have to take into account is that the GUI thread (the one that is drawing yours widgets onto the screen) is independant.
You can not ask, within a thread, to draw something into the screen.

brott
7th March 2010, 16:57
QThread:run() : Returning from this method will end the execution of the thread

Hi, Thanks for your answer.

A Returing (meaning 'return 0;' eg.) seems to be complicated under most circumstances.
Just to make sure, that I am right: when I call the run-function, the thread starts to work and with the exec(); it stays running? So what about quit() or exit()?

toutarrive
7th March 2010, 17:58
Create an instance of the thread object and call QThread::start().
The code that appears in you run() reimplementation will then be executed in a separate thread.
There is no need to call the run function directly