Results 1 to 3 of 3

Thread: Simple QThread class and QObject::moveToThread() - questions

  1. #1
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Simple QThread class and QObject::moveToThread() - questions

    Hi all,

    I'm new to threading in Qt.
    I have several questions regarding the following class. I hope that wise programmers of this forum will help me better understand the QThread class and how does threads works in Qt.

    thread1.h
    Qt Code:
    1. #ifndef THREAD1_H
    2. #define THREAD1_H
    3.  
    4. #include <QThread>
    5. #include <QTimer>
    6.  
    7. class Thread1 : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. Thread1();
    12. ~Thread1();
    13.  
    14. protected:
    15. void run();
    16.  
    17. signals:
    18. void sigThreadQuit();
    19.  
    20. public slots:
    21. void sTimeout();
    22.  
    23. private:
    24. QTimer* timer1;
    25. QTimer timer2;
    26.  
    27. int counter;
    28. };
    29.  
    30. #endif // THREAD1_H
    To copy to clipboard, switch view to plain text mode 

    thread1.cpp
    Qt Code:
    1. #include "thread1.h"
    2.  
    3. Thread1::Thread1():
    4. timer1(new QTimer(this)), counter(0)
    5. {
    6. qDebug("Thread1(): Creating...");
    7. timer1->setInterval(1000);
    8. timer2.setInterval(1000);
    9.  
    10. connect(timer1, SIGNAL(timeout()), this, SLOT(sTimeout()));
    11. connect(&timer2, SIGNAL(timeout()), this, SLOT(sTimeout()));
    12.  
    13. moveToThread(this);
    14. }
    15.  
    16. Thread1::~Thread1() {
    17. qDebug("~Thread1(): Destroyed.");
    18. }
    19.  
    20. void Thread1::run() {
    21. qDebug("Thread1::run(): Running...");
    22. timer1->start();
    23. timer2.start();
    24. exec();
    25. }
    26.  
    27. void Thread1::sTimeout() {
    28. QTimer* t = (QTimer*) sender();
    29. qDebug("sTimeout(): Timer: %p; Counter: %d", t, counter);
    30. if(counter > 10)
    31. emit sigThreadQuit();
    32. counter++;
    33. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include "thread1.h"
    4. #include "thread2.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9. qDebug("Main start");
    10.  
    11. // Thread1* t1 = new Thread1();
    12.  
    13. Thread1 t2;
    14.  
    15. // Thread2 t2;
    16.  
    17. // a.connect(t1, SIGNAL(sigThreadQuit()), &a, SLOT(quit()));
    18. a.connect(&t2, SIGNAL(sigThreadQuit()), &a, SLOT(quit()));
    19.  
    20. // t1->start();
    21. t2.start();
    22.  
    23. int retvel = a.exec();
    24.  
    25. qDebug("Main end");
    26. return retvel;
    27. }
    To copy to clipboard, switch view to plain text mode 

    The main function creates an object of class Thread1, which is designed to display text and after 10 seconds to send a signal to finish the thread.
    After creating a t2 object, signal sending by a thread is connected with the slot quit() of the main application.
    In the above class, I have two objects QTimer - one of them is a pointer.
    After running the application I see that only one timer is working - a pointer.

    Here are my questions:
    1. Why I can not see the operation of the QTimer object which is not a pointer? Does is not running?
    2. If I don't use function moveToThread() for instance in the constructor of Thread1 class, then QTimer objects (or a thread), do not start in general. I think that it has something with main a.exec() function. Why and what?
    3. Does the above example is written correctly with the principles of multithreaded programming in Qt?

    I wrote this class for understanding Qt thread support. Any posts with good explanations and links are welcome.

    Thank you for any answers

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simple QThread class and QObject::moveToThread() - questions

    1. Never ever move thread to it self! Moving QObject to thread means that connected (see Qt::ConnectionType-enum) slots will be run in thread they were move to!
    2. moveToThread works only when object doesn't have a parent! See QObject::moveToThread
    3. Read this

  3. #3
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple QThread class and QObject::moveToThread() - questions

    Thank you for your post.

    Third answer has me thinking. So now I have to redesign my class and read once again docs and tutorials carefully.

    Thanks.

Similar Threads

  1. Simple QObject::connect question
    By Bryku in forum Newbie
    Replies: 2
    Last Post: 8th January 2010, 12:09
  2. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37
  3. QObject::moveToThread warning
    By mnemonic_fx in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2007, 22:11
  4. qobject connect questions
    By VireX in forum Newbie
    Replies: 5
    Last Post: 20th May 2007, 09:04
  5. QObject::moveToThread() warnings
    By gri in forum Qt Programming
    Replies: 9
    Last Post: 2nd April 2007, 16:46

Tags for this Thread

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.