Results 1 to 7 of 7

Thread: A class with Qtimer

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default A class with Qtimer

    This is a very newbie ask....
    I don't understand some QT concepts .
    I want to have a Timer into a simple class.

    The h file
    Qt Code:
    1. #ifndef TIMER_H
    2. #define TIMER_H
    3. #include <QTimer>
    4.  
    5. class Timer
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. Timer();
    11. void start();
    12. QTimer * m_timer;
    13.  
    14. public slots:
    15. void updateTime();
    16.  
    17. };
    18.  
    19. #endif // TIMER_H
    To copy to clipboard, switch view to plain text mode 


    the cpp file

    Qt Code:
    1. #include "timer.h"
    2.  
    3. Timer::Timer()
    4. {
    5. }
    6.  
    7. void Timer::start(){
    8. m_timer=new QTimer(this);
    9. m_timer->interval(1000);
    10. connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
    11. }
    12.  
    13. void Timer::updateTime(){
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    I have the errors :
    error: no matching function for call to 'QTimer::QTimer(Timer* const)'
    note: candidates are: QTimer::QTimer(const QTimer&)
    error: 'connect' was not declared in this scope

    Have I to create the Timer class extending QObjetc ?
    What includes have I missing ?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: A class with Qtimer

    Quote Originally Posted by tonnot View Post
    I have the errors :
    error: no matching function for call to 'QTimer::QTimer(Timer* const)'
    note: candidates are: QTimer::QTimer(const QTimer&)
    error: 'connect' was not declared in this scope
    ...which explains eveything
    Have I to create the Timer class extending QObjetc ?
    No, you just cant pass your class to the constructor of QTimer. Pass 0 and delete it in your custom destructor.

    What includes have I missing ?
    none. use
    Qt Code:
    1. QObject::connect();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A class with Qtimer

    Ok, using QTimer(0) runs.
    But, why dont run QTimer(this) ? Because my class is not a Qwidget ?
    (I see the code for tutorial-analogclock and see QTimer(this) ...)

    And , connect keep on failing....

    Please, a little bit more help....
    I only want to have a Qtimer into a basic class....

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A class with Qtimer

    Ok, using QTimer(0) runs.
    But, why dont run QTimer(this) ? Because my class is not a Qwidget ?
    No.
    Look at the QTimer constructor.
    What type of parameter is it looking for?
    What type did you give it?

    I only want to have a Qtimer into a basic class....
    Do you want a class which IS a timer, or a class that USES a timer?
    QTimer is a timer, so probably you don't need to reinvent the wheel.
    For using it, just declare a QTimer member variable in the class where you want to use it.

    Why do did you declare the Q_OBJECT macro in your class declaration?
    Do you know what is it for?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A class with Qtimer

    I want a class that uses a timer.
    If I dont declare Q_OBJECT macro I cant have slots....

    Connect does not work :
    QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
    error: no matching function for call to 'QObject::connect(QTimer*&, const char*, Timer* const, const char*)'

    neither
    m_timer->singleShot(600000, this, SLOT(Timer::updatetime()));
    m_timer->singleShot(600000, this, SLOT(updatetime()));

    error: no matching function for call to 'QTimer::singleShot(int, Timer* const, const char*)'

    Please, as you can see I have trouble with C++ and QT concepts, but if anyone explain me how to do this I think I'm going to open my mind to QT...
    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A class with Qtimer

    If I dont declare Q_OBJECT macro I cant have slots....
    This is only half the truth, and is the cause of your other problems.
    What are the conditions that need to be fulfilled to be able to use signals and slots?
    Answer this, and you will know why you have the other problems you just posted.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    tonnot (9th February 2011)

  8. #7
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A class with Qtimer

    Thank you H_F.
    I have reading QObject....
    Now all works....
    Thanks again

Similar Threads

  1. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  2. Replies: 2
    Last Post: 9th September 2009, 00:26
  3. Replies: 3
    Last Post: 27th December 2008, 19:34
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.