PDA

View Full Version : Timer not connecting to private SLOT



been_1990
15th December 2009, 02:28
This is my header:

#ifndef ALARMH_H
#define ALARMH_H

class Alarm : public QObject
{
Q_OBJECT

public:
Alarm(QString file)
{
// code here
}
int parseXML(QDomDocument doc, QString file)
{
// code here
}
void checkAlarm()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkTime()));
qDebug() << timer->timerId();
timer->start(100);
}
private:
QString alarmName;
QTime alarmTime;
QDate alarmDate;
QString alarmSound;
QString alarmMessage;
QString alarmApp;
QString alarmAppArg;

signals:
void soundAlarm();

private slots:
void checkTime()
{
qDebug() << "check ....";
}


};
#endif // ALARMH_H

I use it in .cpp:

Alarm a(filename);
a.checkAlarm();

But the timer somehow does not call the SLOT once it's timed out. Probably my mistake, but where?

yogeshgokul
15th December 2009, 05:53
Please create your timer variable class level. So now your header will be:


#ifndef ALARMH_H
#define ALARMH_H

class Alarm : public QObject
{
Q_OBJECT
QTimer *timer;
public:
Alarm(QString file)
{
// code here
}
int parseXML(QDomDocument doc, QString file)
{
// code here
}
void checkAlarm()
{
timer = new QTimer(this);
if(!connect(timer, SIGNAL(timeout()), this, SLOT(checkTime()))
{
qDebug()<<"Critical!! Cannot connect";
}

qDebug() << timer->timerId();
timer->start(100);
}
private:
QString alarmName;
QTime alarmTime;
QDate alarmDate;
QString alarmSound;
QString alarmMessage;
QString alarmApp;
QString alarmAppArg;

signals:
void soundAlarm();

private slots:
void checkTime()
{
qDebug() << "check ....";
}


};
#endif // ALARMH_H

been_1990
16th December 2009, 00:25
Still no sign of the SLOT being called.

wysota
16th December 2009, 00:39
You are creating your object on the stack. If it's inside some scope, like a function or method other than main, it will go out of scope and be destroyed.

been_1990
16th December 2009, 01:07
Ok, I finnaly understand the whole heap/stack stuff now. Thanks wysota.
It's working now.
Off topic:
What do you think about the Class? Will having , let's say, 20 new Alarm classes affect the programs usage of computers rescources to much?