Results 1 to 10 of 10

Thread: Thread problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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, 06:13
  2. Replies: 11
    Last Post: 7th July 2006, 13:09
  3. Thread problem with windows
    By vratojr in forum Qt Programming
    Replies: 17
    Last Post: 16th June 2006, 08:34
  4. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12: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, 11: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
  •  
Qt is a trademark of The Qt Company.