Results 1 to 9 of 9

Thread: Creating Threads

  1. #1
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Thumbs down Creating Threads

    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.

    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. MyThread()
    5. {};
    6.  
    7. void run()
    8. { printf("Working");};
    9.  
    10. void stop(){};
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Please help me on this .....
    Last edited by machathu; 27th February 2006 at 04:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Creating Threads

    I think your run() must be in protected block....it first..second - how you did include yhem?
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating Threads

    Take a look at QThread documentation
    run() should be declared as virtual!

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Creating Threads

    Quote Originally Posted by Cesar
    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.
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating Threads

    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:
    Qt Code:
    1. //Unit1.cpp
    2. #include "MyThread.h"
    3. //Some Unit1 code
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //Unit2.cpp
    2. #include "MyThread.h"
    3. //Some Unit2 code
    To copy to clipboard, switch view to plain text mode 
    Both of these files contain implementation of MyThread::run(). That causes no errors while compiling. But! When you try to link both [B]Unit1.o[/I] and 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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating Threads

    Quote Originally Posted by Cesar
    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:

    Qt Code:
    1. #ifndef __MYTHREAD_H
    2. #define __MYTHREAD_H
    3. class MyThread : public QThread {
    4. //...
    5. };
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 27th February 2006 at 11:32.

  7. #7
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Thumbs up Re: Creating Threads

    Thak you everyone for your help. I'll try to rewrite the code and run.

  8. #8
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Post QT Threads

    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.
    Attached Files Attached Files

  9. #9
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT Threads

    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
    Attached Files Attached Files
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. Problem creating a QWidget using threads
    By tarod in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2007, 12:45
  4. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.