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>
{
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;
};
#endif
#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
To copy to clipboard, switch view to plain text mode
-----------------------------------------------------------
timer.cpp
#include <QtGui>
#include "timer.h"
{
hour = 0;
minute = 0;
second = 0;
}
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
-----------------------------------------------
main.cpp
...
Timer *t = new Timer();
QObject::connect(qTimer,
SLOT(timeout
()), t,
SIGNAL(addSecond
()));
QObject::connect(t,
SLOT(secondChanged
(int)), l,
SIGNAL(setNum
(int)));
qTimer->start(1000);
l->show();
...
...
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();
...
To copy to clipboard, switch view to plain text mode
Bookmarks