Reputation: 293
I have made a timer using QLCDNumber to be displayed on my status bar. I have tried to connect to my slot. However, it seems like the slot never ever get called. Can anyone advise me on what has gone wrong?
Thanks.
By the way, if I were to use this approach, will the timer be counting up according to how a clock time is ticking? (i.e. when the seconds reach 59, it will reset to 0, and minutes will turn 1.)
My .h:
class StatusBar : public QStatusBar, QLCDNumber
{
Q_OBJECT
public:
StatusBar()
{
m_simulation = 0;
createButtons();
};
~StatusBar() { };
public slots:
void countUp();
private:
QLCDNumber *lcdSimulation;
int m_simulation;
QTimer *simulationTimer;
};
My .cpp:
void StatusBar::createButtons()
{
...
lcdSimulation = new QLCDNumber;
lcdSimulation->setNumDigits(8);
lcdSimulation->display("12:00:00");
simulationTimer = new QTimer;
simulationTimer->start(5000);
QObject::connect(simulationTimer, SIGNAL(timeout()), lcdSimulation, SLOT(countUp()));
addWidget(lcdSimulation);
...
}
void StatusBar::countUp()
{
m_simulation++;
lcdSimulation->display(m_simulation);
}
Upvotes: 1
Views: 1680
Reputation: 7132
Hard to say like that but I would check that :
this
, not lcdNumber
Reworked code:
bool b=QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(countUp()));
Q_ASSERT(b);
Upvotes: 1
Reputation: 21220
The problem seems to be in incorrect target object for the signal.
Wrong
QObject::connect(simulationTimer, SIGNAL(timeout()), lcdSimulation, SLOT(countUp()));
Correct
QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(countUp()));
Because your countUp()
defined in StatusBar
class and not in QLCDNumber
.
Upvotes: 3