Wallace
Wallace

Reputation: 293

Qt QLCDNumber Timer

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

Answers (2)

Bruce
Bruce

Reputation: 7132

Hard to say like that but I would check that :

  • your header file is correctly moc-ed (check that a moc_statusbar.cpp is generated, compiled and linked)
  • your StatusBar class composes (*lcdSimulation) and derivates from QLCDNumber : you should choose which one is the best (composition most probably)
  • connect returns a boolean : you should assert on this. If asserts fails, check console to know what happened (a trace is emitted on mismatch)
  • in your case, the slot is defined in this, not lcdNumber

Reworked code:

bool b=QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(countUp()));
Q_ASSERT(b);

Upvotes: 1

vahancho
vahancho

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

Related Questions