Results 1 to 5 of 5

Thread: QTimer Usage in C++ Unix / Windows

  1. #1
    Join Date
    May 2017
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QTimer Usage in C++ Unix / Windows

    Hello everyone,

    This is a basic issue, but i didnt understand why I am failed timer slots. I used Qt 5.7 in console application timer isn't fired timeout signal.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "mytimer.h"
    3. #include <QThread>
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. MyTimer* myTimer = new MyTimer();
    9.  
    10. while(true)
    11. {
    12. QThread::yieldCurrentThread();
    13. }
    14.  
    15. return a.exec();
    16. }
    17.  
    18.  
    19. #ifndef MYTIMER_H
    20. #define MYTIMER_H
    21.  
    22. #include <QObject>
    23. #include <QTimer>
    24. class MyTimer : public QObject
    25. {
    26. Q_OBJECT
    27. public:
    28. explicit MyTimer(QObject *parent = 0);
    29. QTimer* timerObj;
    30. signals:
    31.  
    32. public slots:
    33. void TimerTick();
    34. };
    35.  
    36. #endif // MYTIMER_H
    37.  
    38. #include "mytimer.h"
    39. #include "iostream"
    40. MyTimer::MyTimer(QObject *parent) : QObject(parent)
    41. {
    42. timerObj = new QTimer(this);
    43. timerObj->setInterval(1000);
    44. connect(timerObj,SIGNAL(timeout()),this,SLOT(TimerTick()));
    45. timerObj->start();
    46. }
    47.  
    48. void MyTimer::TimerTick()
    49. {
    50. std::cout << "Timer Interrupt" << std::endl;
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rcanerA; 12th May 2017 at 20:13.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer Usage in C++ Unix / Windows

    Your timer does not start running until you call QApplication::exec() (line 15 in your main.cpp) because timers need an event loop and the event loop isn't started until exec() is called. Your infinite while() loop prevents this from ever happening.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2017
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer Usage in C++ Unix / Windows

    Thanks it is working now however i dont understand totally. a.exec() is used for what ?

    This exec() is like as a thread run function. Am I right?

    And also when I dont use QCoreApplication, can timer work anyway?
    I mean QCoreApplication a(argc,argv) and a.exec() removed from the main. Rest of the code will be same.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer Usage in C++ Unix / Windows

    And also when I dont use QCoreApplication, can timer work anyway?
    No. QApplication is what runs the event loop, and everything in Qt depends on an active event loop. You cannot have a Qt app without a QApplication class and exec().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    May 2017
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer Usage in C++ Unix / Windows

    Thnak you so much.

Similar Threads

  1. QPixmap/QImage serious issues between Windows/Unix
    By Largo in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2011, 13:22
  2. Border radius unix/windows
    By dacrawler in forum Newbie
    Replies: 2
    Last Post: 23rd January 2011, 13:27
  3. Emulating Unix on Windows
    By eichner in forum Qt Programming
    Replies: 5
    Last Post: 14th February 2008, 18:13
  4. distinguish windows and unix in Makefile
    By magland in forum General Programming
    Replies: 1
    Last Post: 4th April 2007, 19:43
  5. converting unix exe to windows binary
    By deekayt in forum General Programming
    Replies: 2
    Last Post: 17th September 2006, 01:00

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.