PDA

View Full Version : Creating Threads



machathu
27th February 2006, 03:18
Hi everyone,

I'm new to QT programming.
I try to create a threaded program using Qt3's QThread class. I create a class MyThread and try to start it from a main widget. But when I try to compile it gives linking errors saying that multiple diclarations for MyThread::run() and MyThread::stop(). How can I solve this problem.


class MyThread : public QThread
{
public:
MyThread()
{};

void run()
{ printf("Working");};

void stop(){};

}


Please help me on this .....

zlatko
27th February 2006, 08:07
I think your run() must be in protected block....it first..second - how you did include yhem?

Cesar
27th February 2006, 08:24
Take a look at QThread (http://doc.trolltech.com/3.3/qthread.html) documentation ;)
run() should be declared as virtual!

zlatko
27th February 2006, 08:35
run() should be declared as virtual!

It declare as virtual in QThread class so in your child class you dont must use keyword "virtual" but you must declare it as protected necessarily.

Cesar
27th February 2006, 10:10
First of all I'd like to apologize: my topic about virtual doesn't make any sense.
And now about the problem itself. Consider such an example:


//Unit1.cpp
#include "MyThread.h"
//Some Unit1 code



//Unit2.cpp
#include "MyThread.h"
//Some Unit2 code

Both of these files contain implementation of MyThread::run(). That causes no errors while compiling. But! When you try to link both Unit1.o[/I] and [B]Unit2.o you get the error saying about multiply declarations of MyThread::run(). The solution is to split MyThread.h into two files: MyThread.h, containing only the declaration, and MyThread.cpp, containing the implementation. This way you get MyThread::run() compiled once and you can safely link against MyThread.o

wysota
27th February 2006, 10:21
The solution is to split MyThread.h into two files: MyThread.h, containing only the declaration, and MyThread.cpp, containing the implementation. This way you get MyThread::run() compiled once and you can safely link against MyThread.o

Hmm... what about:


#ifndef __MYTHREAD_H
#define __MYTHREAD_H
class MyThread : public QThread {
//...
};
#endif

I hope you realise there is no stop() method in QThread.

Your code by itself looks fine, just lacks the #ifndef clause and you should probably move that run() method body to a separate file when it grows :)

machathu
27th February 2006, 10:37
Thak you everyone:) for your help. I'll try to rewrite the code and run.

machathu
28th February 2006, 02:54
Hi friends,
I have write 2 programs that uses threads. One is using QT3 designer and other one is hand coded.
The program I written using QT3 designer gives linking error saying multiple definition of `MyThread::run()'(please see error.txt for the comlete error message).
But the other program that I hand coded works fine.
I posted a message on this yesterday as well.Sorry I couldn't give you all the details yesterday. But with this I have attached both program here.

Please help me on this.

sunil.thaha
28th February 2006, 05:41
Well I compile both - Test and the Test1 and found that none of them complied correctly.
I have corrected the test. Added the thread in Config section of the .pro file. and in the frmmain.h . replace include myThread.cpp to myThread.h. Well i removed the virtual keyword from the Mythread class.( Found No use of it ) . And prevented the multiple inclusion of the header ... I have attached the Test.tar.gz . Just Go through