Results 1 to 10 of 10

Thread: Thread problem

  1. #1
    Join Date
    Aug 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread problem

    Hello,

    I can't run all the QThread applications. The problem is the same. Maybe the compiler need some configuration. I have the Qt 4.1.1 open source for windows XP. The problem is the following:

    C:\Qt\4.1.1\MyThread>make
    mingw32-make -f Makefile.Debug all
    mingw32-make[1]: Entering directory `C:/Qt/4.1.1/MyThread'
    g++ -c -g -g -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL
    -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"C:/Qt/4.1.1/
    include/QtCore" -I"C:/Qt/4.1.1/include/QtGui" -I"C:/Qt/4.1.1/include" -I"." -I"C
    :/Qt/4.1.1/include/ActiveQt" -I"tmp\moc\debug_shared" -I"." -I"C:/Qt/4.1.1/mkspe
    cs/win32-g++" -o tmp\obj\debug_shared\main.o main.cpp
    main.cpp:26:2: warning: no newline at end of file
    g++ -mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runt
    ime-pseudo-reloc -Wl,-subsystem,windows -o "debug\MyThread.exe" tmp\obj\debug_sh
    ared\main.o -L"C:\Qt\4.1.1\lib" -L"C:\Qt\4.1.1\lib" -lmingw32 -lqtmaind -lQtGui
    d4 -lQtCored4
    tmp\obj\debug_shared\main.o(.text$_ZN8MyThreadD1Ev[MyThread::~MyThread()]+0xb):
    In function `ZN8MyThreadD1Ev':
    C:/Qt/4.1.1/MyThread/main.cpp: undefined reference to `vtable for MyThread'
    tmp\obj\debug_shared\main.o(.text$_ZN8MyThreadC1Ev[MyThread::MyThread()]+0x20):C
    :/Qt/4.1.1/MyThread/main.cpp: undefined reference to `vtable for MyThread'
    collect2: ld returned 1 exit status
    mingw32-make[1]: *** [debug\MyThread.exe] Error 1
    mingw32-make[1]: Leaving directory `C:/Qt/4.1.1/MyThread'
    mingw32-make: *** [debug-all] Error 2

    Thanks,
    Panos

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem

    You have to declare macro Q_OBJECT in the MyThread Body

    Qt Code:
    1. class MyThread: ....
    2. {
    3. Q_OBJECT
    4. ......
    5. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Aug 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread problem

    I have already done that. I don't think that this is the problem.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread problem

    • Do both MyThread::MyThread() and MyThread::~MyThread() actually have an implementation body?
    • Is mythread.h listed in .pro?
    • Did you re-run qmake after adding Q_OBJECT?
    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread problem

    I have tried all these but the problem remais. I think that the real problem is somewhere here "undefined reference to `vtable for ThreadDialog' ", but I don't know what to do.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem

    Can you post the code?

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread problem

    Quote Originally Posted by panos View Post
    I have tried all these but the problem remais. I think that the real problem is somewhere here "undefined reference to `vtable for ThreadDialog' ", but I don't know what to do.
    But this is a different error than the one posted above. Repeat the same steps for ThreadDialog.
    J-P Nurmi

  8. #8
    Join Date
    Aug 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread problem

    I am sorry but I have deleted the previous code. So I post here another example that I tried to run:

    #include <QApplication>
    #include <QThread>

    #include <iostream>

    using namespace std;

    class Producer : public QThread
    {
    Q_OBJECT
    public:
    void run();
    };

    void Producer::run()
    {
    cout<<"Bye!"<<endl;
    }

    class Consumer : public QThread
    {
    Q_OBJECT
    public:
    void run();
    };

    void Consumer::run()
    {
    cout<<"Hello!"<<endl;
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    Producer producer;
    Consumer consumer;
    producer.start();
    consumer.start();
    producer.wait();
    consumer.wait();
    return app.exec();
    }

    By the way I would like to ask you how can I write something in the console (like when using the "cout" in C++)

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread problem

    The problem goes away once you declare classes containing Q_OBJECT macro in header files. Whenever qmake is run, it parses the project's header files and generates make rules to invoke moc for those files that contain a Q_OBJECT macro.

    producer.h
    Qt Code:
    1. class Producer : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run();
    6. };
    To copy to clipboard, switch view to plain text mode 
    producer.cpp
    Qt Code:
    1. void Producer::run()
    2. {
    3. cout<<"Bye!"<<endl;
    4. }
    To copy to clipboard, switch view to plain text mode 
    This ensures that moc gets run as required. Notice that corresponding files must be listed in .pro file and you must always re-run qmake after adding Q_OBJECT macro to any class. Notice also that you need Q_OBJECT only if you declare custom signals and/or slots, or use other meta object stuff.

    Now, if you want to put everything in a .cpp file (which is useful for minimal examples etc.) you must include the moc file by hand (and once again re-run qmake so that qmake detects it and creates proper make rules for moc):

    main.cpp
    Qt Code:
    1. class Producer : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run()
    6. {
    7. cout<<"Bye!"<<endl;
    8. }
    9. };
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. ...
    14. }
    15.  
    16. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    For more details about moc, please consult moc docs.

    Quote Originally Posted by panos View Post
    By the way I would like to ask you how can I write something in the console (like when using the "cout" in C++)
    On windows, you must add
    CONFIG += console
    to the .pro file (and rebuild the project) to be able to output to console. Oh, and there is qDebug() which is aware of most Qt data types, see debugging techniques for more details.
    J-P Nurmi

  10. #10
    Join Date
    Aug 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Thread problem

    Thank you very much!
    That worked for me!

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  2. Replies: 11
    Last Post: 7th July 2006, 14:09
  3. Thread problem with windows
    By vratojr in forum Qt Programming
    Replies: 17
    Last Post: 16th June 2006, 09:34
  4. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 13:05
  5. Problem building Qt4.1.0 with thread support on windows XP
    By pavithra in forum Installation and Deployment
    Replies: 1
    Last Post: 1st April 2006, 12:35

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.