PDA

View Full Version : Counter widget



eu.x
27th February 2007, 18:29
Hi,

I'm trying to make a widget to handle time.

timer.h


#ifndef Timer_H
#define Timer_H

#include <QWidget>
class QLabel;

class Timer : public QWidget
{
Q_OBJECT

public:
Timer();
int getHour();
int getMinute();
int getSecond();

int secondValue() const;

void show();

public slots:
void addSecond();

signals:
void secondChanged(int);

private:
int hour;
int minute;
int second;

QLabel *l;
};

#endif

-----------------------------------------------------

timer.cpp


#include <QtGui>
#include "timer.h"

Timer::Timer() : QWidget()
{
hour = 0;
minute = 0;
second = 0;

l = new QLabel();
}

void Timer::addSecond()
{
second++;

if(second == 60)
{
minute++;
second = 0;
if(minute == 60)
{
hour++;
minute = 0;
}
}

emit secondChanged(second);
}

int Timer::getHour()
{
return hour;
}

int Timer::getMinute()
{
return minute;
}

int Timer::getSecond()
{
return second;
}

int Timer::secondValue() const
{
return second;
}

void Timer::secondChanged(int secondValue)
{
l->setNum(secondValue);
}

void Timer::show()
{
l->show();
}


I'm getting this error:

debug\moc_timer.o(.text+0x1e0): In function `ZN5Timer13secondChangedEi':
C:/Projetos/backlog/debug/moc_timer.cpp:77: multiple definition of `Timer::secon
dChanged(int)'
debug\timer.o(.text+0x46a):C:/Projetos/backlog/timer.cpp:52: first defined here
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\backlog.exe] Error 1
mingw32-make[1]: Leaving directory `C:/Projetos/backlog'
mingw32-make: *** [debug] Error 2

What should I do?!?

jpn
27th February 2007, 18:34
You don't implement function body for signals (the meta object compiler does it for you behind the curtains). So the declaration of secondChanged() is enough.

PS. Please use [ code ] -tags around code blocks to make them more readable. :)

eu.x
27th February 2007, 18:47
So how do I change values on other objects? For example, I need to update a QLabel everytime secondChanged signal is called.

My new code is:

timer.h


#ifndef Timer_H
#define Timer_H

#include <QWidget>
class QLabel;

class Timer : public QWidget
{
Q_OBJECT

public:
Timer();
int getHour();
int getMinute();
int getSecond();

int secondValue() const;

void show();

public slots:
void addSecond();

signals:
void secondChanged(int);

private:
int hour;
int minute;
int second;

QLabel *l;
};

#endif


-----------------------------------------------------------

timer.cpp


#include <QtGui>
#include "timer.h"

Timer::Timer() : QWidget()
{
hour = 0;
minute = 0;
second = 0;

l = new QLabel();
}

void Timer::addSecond()
{
second++;
if(second == 60)
{
minute++;
second = 0;
if(minute == 60)
{
hour++;
minute = 0;
}
}
emit secondChanged(second);
}

int Timer::getHour()
{
return hour;
}

int Timer::getMinute()
{
return minute;
}

int Timer::getSecond()
{
return second;
}

int Timer::secondValue() const
{
return second;
}

void Timer::show()
{
l->show();
}


-----------------------------------------------

main.cpp


...
Timer *t = new Timer();
QTimer *qTimer = new QTimer();
QObject::connect(qTimer, SLOT(timeout()), t, SIGNAL(addSecond()));
QObject::connect(t, SLOT(secondChanged(int)), l, SIGNAL(setNum(int)));
qTimer->start(1000);
l->show();
...

wysota
27th February 2007, 18:57
I'd suggest using the timerEvent here instead of a standalone QTimer object.

eu.x
27th February 2007, 19:02
I'd suggest using the timerEvent here instead of a standalone QTimer object.
Do you have some example?

wysota
27th February 2007, 19:40
Well, your widget is so simple it might serve as an example :)

Please take a look at QObject::timerEvent() reference. It has all you need along an example use of startTimer() and timerEvent().

jacek
27th February 2007, 19:48
For example, I need to update a QLabel everytime secondChanged signal is called.
You don't call signals --- you emit them. If you want to react on a signal, you have to connect it to a slot and do what you need in that slot.



QObject::connect(qTimer, SLOT(timeout()), t, SIGNAL(addSecond()));
QObject::connect(t, SLOT(secondChanged(int)), l, SIGNAL(setNum(int)));
It should be:
QObject::connect( qTimer, SIGNAL(timeout()), t, SLOT(addSecond()) );
QObject::connect( t, SIGNAL(secondChanged(int)), l, SLOT(setNum(int)) );

eu.x
27th February 2007, 20:30
Thank you jacek, I was confusing slots and signals.