Qt QLCDNumber time_t Timer

时间:2013-10-22 10:11:56

标签: c++ qt timer

我在一个.cpp中有一个工具按钮(即ExerciseControl.cpp)。单击该按钮时,它将在另一个.cpp(即StatusBar.cpp)中触发两个计时器。

但是,以下代码不起作用。单击ExerciseControl.cpp中的按钮以触发startExercise()时,计时器无法启动。

ExerciseControl.cpp:
ExerciseControl::ExerciseControl(void)
{
    myStatusBar = new StatusBar;
}

void ExerciseControl::startExercise()
{
    myStatusBar ->simulationTimer->start(1000);
    myStatusBar ->elapsedTimer->start(1000);
}


StatusBar.cpp:
StatusBar::StatusBar()
{
    simulationTimer = new QTimer;
    QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
    elapsedTimer = new QTimer;
    QObject::connect(elapsedTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));

    createButtons();
};

void StatusBar::createButtons()
{
    ...
}

void StatusBar::tickSimulation()
{
    ...
}

但是,如果我在构造函数中声明了以下内容,那么定时器可以自动启动,这不是我想要的。

StatusBar::StatusBar()
{
    simulationTimer = new QTimer;
    simulationTimer->start(1000);
    QObject::connect(simulationTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
    elapsedTimer = new QTimer;
    elapsedTimer->start(1000);
    QObject::connect(elapsedTimer, SIGNAL(timeout()), this, SLOT(tickSimulation()));
}

我需要通过ExerciseControl.cpp中的按钮触发计时器。

请帮忙。

感谢。

1 个答案:

答案 0 :(得分:0)

在下面的示例中,ExerciseControlQMainWindow的一个专门化,可以做两件事:

  • 在窗口内显示QTooButton;
  • 分配StatusBar个对象。

enter image description here

单击该按钮可调用该类的插槽 _timers_must_start()。此广告负责从start_timers()对象调用StatusBar,后者又会启动该对象的计时器。

我将每个计时器调用一个插槽,在触发timeout()时将某些内容打印到屏幕上。

享受。

<强>的main.cpp

#include "exercise_control.h"
#include <QApplication>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    ExerciseControl ec;
    ec.show();

    return app.exec();
}

<强> exercise_control.h

#pragma once
#include "status_bar.h"
#include <QMainWindow>
#include <QToolButton>

class ExerciseControl : public QMainWindow
{
    Q_OBJECT
public:
    ExerciseControl()
    {
        _button = new QToolButton(this);
        _button->setText("Press Me!");
        QObject::connect(_button, SIGNAL(clicked()), this, SLOT(_timers_must_start()));

        _statusbar = new StatusBar;

        // Set the size of the window to the size of the button
        setFixedSize(_button->width() ,_button->height());
    }

private slots:
    /* This slot is called by clicking the button */
    void _timers_must_start()
    {
        qDebug() << "ExerciseControl::_timers_must_start: button clicked! Trying to start timers...";
        _statusbar->start_timers();
    }

private:
    QToolButton* _button;
    StatusBar* _statusbar;
};

<强> status_bar.h

#pragma once
#include <QObject>
#include <QTimer>
#include <QDebug>

class StatusBar : public QObject
{
    Q_OBJECT
public:
    StatusBar()
    {
        _timer1 = new QTimer;
        QObject::connect(_timer1, SIGNAL(timeout()), this, SLOT(_timer1_task()));

        _timer2 = new QTimer;
        QObject::connect(_timer2, SIGNAL(timeout()), this, SLOT(_timer2_task()));
    }

    ~StatusBar()
    {
        _timer1->stop();
        delete _timer1;

        _timer2->stop();
        delete _timer2;
    }

    void start_timers()
    {
        if (!_timer1->isActive() && !_timer2->isActive())
        {
            qDebug() << "StatusBar::start_timers: OK!";
            _timer1->start(1000); // 1 second interval
            _timer2->start(5000); // 5 seconds interval
            return;
        }

        qDebug() << "StatusBar::start_timers: timers already started, nothing to be done here!";
    }

private:
    QTimer* _timer1;
    QTimer* _timer2;

private slots:
    /* These slots are called by the timers of this class, hence PRIVATE SLOTS */
    void _timer1_task()
    {
        qDebug() << "StatusBar::_timer1_task: 1 second has elapsed";
    }

    void _timer2_task()
    {
        qDebug() << "StatusBar::_timer2_task: 5 seconds has elapsed";
    }
};

<强> test.pro

QT += widgets

SOURCES += \
    main.cpp

HEADERS += \
    exercise_control.h \
    status_bar.h