PDA

View Full Version : QObject and alarm clock



been_1990
12th December 2009, 22:52
I am making a alarm clock by creating a class Alarm:

#ifndef ALARMH_H
#define ALARMH_H
#include <QString>
#include <QTime>
#include <QDate>

class Alarm : public QObject
{
Q_OBJECT

public:
QString alarmName;
QTime alarmTime;
QDate alarmDate;
QString alarmSound;
QString alarmMessage;
QString alarmApp;
QString alarmAppArg;
};

#endif // ALARMH_H
So I was wondering what is a better approach, to have a function checking on each new Alarm class instance created:

Alarm a1,a2,a3;
a1.setObjectName("a1");
a2.setObjectName("a2");
a3.setObjectName("a3");
QObjectList alarmList;
alarmList << a1 << a2 << a3;

void checkAlarm(QObjectList *list)
{
for(int i = 0;i <= list.count();i++){
check_if_its_time(list.at(i));
}
}
Or if my class should check itself for the time and check if its time to ring the alarm:

#ifndef ALARMH_H
#define ALARMH_H
#include <QString>
#include <QTime>
#include <QDate>

class Alarm : public QObject
{
Q_OBJECT

public:
void checkTime(){
// insert checking the time code
emit soundAlarm();
}
private:
QString alarmName;
QTime alarmTime;
QDate alarmDate;
QString alarmSound;
QString alarmMessage;
QString alarmApp;
QString alarmAppArg;

signals:
soundAlarm();
};

#endif // ALARMH_H

// then in .cpp file I deal with the signal

I hope you guys can understand what I mean..:o Above code may not be compilable, may have let some things out as Im in a hurry and just trying to give an idea. :D

bood
13th December 2009, 16:51
Hmmm...I would choose to implement it inside the Alarm class, seems more flexible

squidge
13th December 2009, 17:00
Allow the alarm clock to be configured via its constructor or a setter method, and then the alarm clock looks after itself and fires the alarm signal when it's time.