Results 1 to 4 of 4

Thread: QTimer doesnt work or complains about thread

  1. #1
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default QTimer doesnt work or complains about thread

    i got following code

    Qt Code:
    1. class freez_session : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit freez_session(QObject *parent = 0);
    6. void run();
    7.  
    8. private slots:
    9. void uploadfullmap();
    10. }
    11.  
    12. void freez_session::run()
    13. {
    14. QTimer *tr = new QTimer();
    15. connect(tr, SIGNAL(timeout()), this, SLOT(uploadfullmap()));
    16. tr->start(500);
    17. ...
    18. }
    19.  
    20. void freez_session::uploadfullmap()
    21. {
    22. qDebug() << "uploading full map";
    23. }
    To copy to clipboard, switch view to plain text mode 

    but uploadfullmap() will never trigger!!

    Qt Code:
    1. when i use QTimer *tr = new QTimer(this);
    To copy to clipboard, switch view to plain text mode 

    i get QObject: Cannot create children for parent that is in different thread...

    where in a thread should be timer put so it work properly?

  2. #2
    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: QTimer doesnt work or complains about thread

    Your first snippet is ok regarding the timer. The problem is probably that you're not running (or you are blocking) the event loop for the main thread which is required for slots in the freez_session object to work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QTimer doesnt work or complains about thread

    despite i dont fully understand why it does/doesnt work, solution is following...

    - i had to remove forever loop and replace it with another timer which will continue call a function 'forever'

    so NO FOREVER/WHILE/FOR(; loops in thread if you want use timers..

  4. #4
    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: QTimer doesnt work or complains about thread

    Quote Originally Posted by daemonna View Post
    despite i dont fully understand why it does/doesnt work, solution is following...
    I told you why it didn't work. You were blocking the event loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    daemonna (1st September 2010)

Similar Threads

  1. connect doesnt work
    By john_god in forum Newbie
    Replies: 4
    Last Post: 23rd November 2008, 09:36
  2. ldapsearch doesnt work.
    By shamik in forum General Discussion
    Replies: 0
    Last Post: 3rd April 2008, 07:25
  3. QFtp...doesnt seems to work ....!!!
    By salmanmanekia in forum Qt Programming
    Replies: 10
    Last Post: 25th February 2008, 08:30
  4. Printing via QPrinter doesnt work
    By mSergey in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 19:00
  5. How come this doesnt work?
    By ShaChris23 in forum Newbie
    Replies: 8
    Last Post: 16th June 2007, 04:43

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.