Results 1 to 4 of 4

Thread: Timer signals in non GUI application

  1. #1
    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 Timer signals in non GUI application

    Hi,

    first of all, sorry for my bad English.
    Now the question.

    I've written a class inherited from QThread
    Qt Code:
    1. // DataSender.cpp
    2.  
    3. DataSender::DataSender(QObject *parent) : QThread(parent)
    4. {
    5. this->m_sendTimer.setInterval (0);
    6.  
    7. connect (&m_sendTimer, SIGNAL(timeout()), this, SLOT(sendData()));
    8. }
    9.  
    10. void DataSender::setSendInterval (int msec)
    11. {
    12. this->m_sendTimer.setInterval (msec);
    13. }
    14.  
    15. void DataSender::run ()
    16. {
    17. bool ans = true;
    18.  
    19. qDebug ("%s Started", qPrintable(this->objectName ()));
    20.  
    21. if (ans && this->m_sendTimer.interval () > 0) {
    22. this->m_sendTimer.start ();
    23. qDebug ("%s: Timer Started", qPrintable(this->objectName ()));
    24. }
    25.  
    26. if (ans) {
    27. this->exec ();
    28. }
    29.  
    30. this->m_sendTimer.stop ();
    31.  
    32. qDebug ("%s Stopped", qPrintable(this->objectName ()));
    33. }
    To copy to clipboard, switch view to plain text mode 

    This class is in a library.

    When I use it in a GUI Application it works
    Qt Code:
    1. void MainWindow::startSenders ()
    2. {
    3. DataSender* s = 0;
    4. for (int i = 0; i < DATA_TYPE_SIZE; ++i) {
    5. s = &this->m_senders[i];
    6. s->setSendInterval (10000);
    7. s->sender->start ();
    8. }
    9. qDebug ("Senders Started");
    10. }
    To copy to clipboard, switch view to plain text mode 

    but when I use it in a non-GUI application
    Qt Code:
    1. // Main.cpp
    2. ...
    3. DataSender* senders[2];
    4.  
    5. senders[0] = new DataSender;
    6. senders[0]->setSendInterval(1000);
    7.  
    8. senders[1] = new DataSender;
    9. senders[1]->setSendInterval(5000);
    10.  
    11. qDebug ("RUNNING...");
    12.  
    13. for (int i = 0; i < 2; ++i) {
    14. senders[i]->start ();
    15. }
    16. result = app.exec ();
    17. ...
    To copy to clipboard, switch view to plain text mode 
    the slot connected to the timeout() signal of timer is never called.
    The debug messages in DataSender.cpp at line 19 and 23 are printed; that means the thread is correctly started.

    Any suggestion?
    A camel can go 14 days without drink,
    I can't!!!

  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: Timer signals in non GUI application

    And you don't get warnings at the console not to start timers from threads different than the ones they were created in? In general I'm more suprised it is working for you when the application has a GUI than that it doesn't work when there is no GUI.
    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. The following user says thank you to wysota for this useful post:

    mcosta (17th September 2010)

  4. #3
    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: Timer signals in non GUI application

    Hi wysota, thanks for the answer.

    I modified the code in way to create QTimer in QThread::run() and it works.

    The question is:
    Why the GUI version works? (The warning messages was shown both in GUI and non-GUI version)
    A camel can go 14 days without drink,
    I can't!!!

  5. #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: Timer signals in non GUI application

    Quote Originally Posted by mcosta View Post
    The question is:
    Why the GUI version works?
    No idea. Maybe there are different safe guards in QCoreApplication and QApplication or maybe it's just pure coincidence.
    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.


Similar Threads

  1. Replies: 8
    Last Post: 29th June 2010, 07:10
  2. Basically signals-slot application
    By Daxos in forum Newbie
    Replies: 4
    Last Post: 27th May 2010, 13:56
  3. Timer delay
    By bmn in forum Qt Programming
    Replies: 11
    Last Post: 3rd June 2008, 11:54
  4. Timer for application
    By Zergi in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2007, 22:21
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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.