PDA

View Full Version : A class with Qtimer



tonnot
9th February 2011, 10:10
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

#ifndef TIMER_H
#define TIMER_H
#include <QTimer>

class Timer
{
Q_OBJECT

public:
Timer();
void start();
QTimer * m_timer;

public slots:
void updateTime();

};

#endif // TIMER_H


the cpp file


#include "timer.h"

Timer::Timer()
{
}

void Timer::start(){
m_timer=new QTimer(this);
m_timer->interval(1000);
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
}

void Timer::updateTime(){

}

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

Lykurg
9th February 2011, 10:17
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
QObject::connect();

tonnot
9th February 2011, 11:21
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....

high_flyer
9th February 2011, 11:26
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?

tonnot
9th February 2011, 14:15
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.

high_flyer
9th February 2011, 14:36
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.

tonnot
9th February 2011, 17:40
Thank you H_F.
I have reading QObject....
Now all works....
Thanks again