Results 1 to 5 of 5

Thread: Problem with QTimer

  1. #1
    Join Date
    Apr 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem with QTimer

    This is what I wrote to make a simple timer.
    openSuse 11, Qt 4.5, Qt Creator, Qt GUI App

    It doesn't work, I mean when I try to execute it "Hi" doesn't appear on Konsole. I tried to debug it in the constructor of the class, but gdb never goes there.
    Thanks for your help

    MyObject.h
    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <QtCore/QTimer>
    3. class MyObject : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. MyObject();
    8. private:
    9. QTimer *timer;
    10.  
    11. private slots:
    12. void timerEvent();
    13. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. MyObject::MyObject()
    2. {
    3. timer = new QTimer(this);
    4. connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
    5. timer->setInterval(10);
    6. timer->start();
    7. }
    8.  
    9. void MyObject::timerEvent()
    10. {
    11. std::cout << "Hi" << std::endl;
    12. timer->start();
    13. }
    14.  
    15. int main()
    16. {
    17. MyObject mo;
    18. sleep(5);
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th April 2009 at 23:32.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QTimer

    of course it will not work. I don't see where you start event loop.
    try this.
    Qt Code:
    1. ...
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QCoreApplication app(argc, argv);
    6. MyObject mo;
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QTimer

    Noooooooooooooooooooooooo

    Sorry for such a stupid question

  4. #4
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QTimer

    And if you derive your class A from some other class B, you should call B's constructor in A():

    Qt Code:
    1. MyObject::MyObject()
    2. : QObject()
    3. {
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th April 2009 at 23:33. Reason: missing [code] tags

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QTimer

    Quote Originally Posted by seim View Post
    And if you derive your class A from some other class B, you should call B's constructor in A():

    Qt Code:
    1. MyObject::MyObject()
    2. : QObject()
    3. {
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 
    actually that happens implicitly. if you want to call a non default base class constructor, you have to say so explicitly using that syntax.
    Last edited by wysota; 11th April 2009 at 23:33.

Similar Threads

  1. Problem with operating of QTimer
    By sudheer168 in forum Newbie
    Replies: 3
    Last Post: 8th October 2008, 20:58
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  3. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  4. QTimer problem
    By vv in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2007, 08:13
  5. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54

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.