对象中的Qtimer不起作用

时间:2018-03-05 10:14:21

标签: c++ qt-creator

我在项目中与QT创作者合作,似乎我一直试图让QTimer工作。

我有一个MainWindow对象,我可以在其中创建和使用QTimers而没有任何问题。在MainWindow中,我创建了一个名为weightChanger的类的对象wc。在wc中,我想要一个连接到wc内几个插槽的计时器。

我尝试了很多不同的方法,但我似乎无法让定时器连接起来。在我的最后一次尝试中,我尝试在我的头文件中为weightChanger类声明计时器:

Picture of header

然后我在wc的构造函数中初始化它们并连接它们。我创建了一个timerTest()插槽,它只是在执行时向控制台发送消息,但我似乎无法以任何方式使其工作。我已经尝试了几种在weightChanger类中实现QTimer的方法,但还没有运气。

CPP file for class weightChanger

任何人都明白我错了什么?

编辑:

这是我的头文件:

#ifndef WEIGHTCHANGER_H
#define WEIGHTCHANGER_H

#include <QObject>
#include <QTimer>
#include "ros_comm.h"
#include <rtl_msgs/target.h>
#include "user.h"


class weightChanger : public QObject
{
    Q_OBJECT
public:
    explicit weightChanger(user* userTemp, const float &ctf, rtl_msgs::state *rtls, QObject *parent = nullptr);


signals:
    void targetForceChanged(float currentTargetForce);


public slots:
    void setRate(const float& ntf);
    void changeForce();
    void rangeOfMotion();
    void changePhase();
    void timerTest();


private:
    QTimer *weightChangeTimer;
    QTimer *changeWeightTimer;
   // rtl_msgs::target* rtlTargets;
    rtl_msgs::state *rtlStates;

    float top;
    float mid;
    float bottom;
    bool concentricPhase = true;

    float currentTargetForce;
    float newTargetForce;
    float rate;
    int numberOfRates = 8;
};

#endif // WEIGHTCHANGER_H

这是我的源文件:

weightChanger::weightChanger(user *userTemp, const float &ctf, rtl_msgs::state *rtls, QObject *parent) :
    top(userTemp->getTop()),
    mid(userTemp->getMid()),
    bottom(userTemp->getBottom()),
    QObject(parent)
{
    ROS_INFO_STREAM("weightchanger ros info work");
    rtlStates = rtls;
    currentTargetForce = ctf;
    weightChangeTimer = new QTimer(this);
    changeWeightTimer = new QTimer(this);
    weightChangeTimer->start(10);
    connect(weightChangeTimer, SIGNAL(timeout()), this, SLOT(timerTest()));
    connect(weightChangeTimer, SIGNAL(timeout()), this, SLOT(changePhase()));
    connect(weightChangeTimer, SIGNAL(timeout()), this, SLOT(rangeOfMotion()));
    connect(changeWeightTimer, SIGNAL(timeout()), this, SLOT(changeForce()));
}

void weightChanger::timerTest()
{
    ROS_INFO_STREAM("timer works");
}

void weightChanger::setRate(const float &ntf)
{
    newTargetForce = ntf;
    rate = (newTargetForce - currentTargetForce) / 8;
    ROS_INFO_STREAM(rate);
    changeWeightTimer->start(10);
}

void weightChanger::changeForce()
{
    if(currentTargetForce != newTargetForce)
    {
        currentTargetForce =+ rate;
        emit targetForceChanged(currentTargetForce);
        ROS_INFO_STREAM(currentTargetForce);
    }
    else
    {
        changeWeightTimer->stop();
        ROS_INFO_STREAM("target force reached");
    }
}

void weightChanger::rangeOfMotion()
{
    float x;

    if(top - rtlStates->position >= mid)
    {
        x = (top - rtlStates->position)/(top - (mid + bottom));
    }
    else
    {
        x = (rtlStates->position - bottom)/((mid + bottom) - bottom);
    }
    emit targetForceChanged((currentTargetForce/2) * x + currentTargetForce);
}


void weightChanger::changePhase()
    {
        if(concentricPhase)
        {
            if(rtlStates->position <= top)
            {
                concentricPhase = false;
                setRate(50);
            }
        }
        else
        {
            if(rtlStates->position >= bottom)
            {
                concentricPhase = true;
                setRate(30);
            }
        }
    }

0 个答案:

没有答案
相关问题