1

我在一个.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 中的按钮触发计时器。

请帮忙。

谢谢。

4

1 回答 1

0

在下面的示例中ExerciseControl它的专业化QMainWindow做了两件事:

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

在此处输入图像描述

单击该按钮将调用该类的插槽 _timers_must_start()。这个负责start_timers()StatusBar对象调用,然后启动该对象的计时器。

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

享受。

主.cpp

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

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

    ExerciseControl ec;
    ec.show();

    return app.exec();
}

运动控制.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;
};

状态栏.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";
    }
};

测试.pro

QT += widgets

SOURCES += \
    main.cpp

HEADERS += \
    exercise_control.h \
    status_bar.h
于 2013-10-25T18:50:14.583 回答