c++ - Qt QLCDNumber time_t 定时器

标签 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 中的按钮触发计时器。

请帮忙。

谢谢。

最佳答案

在下面的例子中ExerciseControlQMainWindow 的特化,它做了两件事:

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

enter image description here

单击按钮调用类的插槽 _timers_must_start()。此 负责从 StatusBar 对象调用 start_timers(),进而启动该对象的计时器。

我让每个计时器调用一个插槽,当 timeout() 被触发时,它会在屏幕上打印一些东西。

享受吧。

ma​​in.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

关于c++ - Qt QLCDNumber time_t 定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514916/

相关文章:

Java定时器和定时器任务: Accessing variables outside Timer

c++ - 如何使用不同的指针算术语义

c++ - ImpersonateLoggedOnUser 并启动使用 ocx 的新进程失败

c++ - ComboBox初始化错误 : Cannot read property 'constructor' of undefined

c++ - 在 Windows Qt Project 上存储外部库的最佳实践

c# - 强制定时器在设定的时间内循环[C#]

c# - 通过 ActiveSync 在 Windows Mobile 上运行应用程序

c++ - 为什么有些类方法返回 "*this"(自身的对象引用)?

c++ - Qt 中的长线

Java 中断/停止计时器