Qt开发(5)——使用QTimer定时触发槽函数
实现效果
软件启动之后,开始计时,到达预定时间后,调用其他类的某个函数。
类的分工
BaseType:软件初始化的调用类
FuncType: 功能函数所在类
具体函数
// FuncType.h
class FuncType: public QObject
{Q_OBJECT
public:
public slots:void slotFunction();
};
// BaseType.h
#include <memory>
#include <QTimer>
#include "FuncType.h"class BaseType: public QObject
{Q_OBJECT
public:
private:QTimer* m_clearTimer;std::shared_ptr<FuncType> m_functype{nullptr};
};
// BaseType.cpp
FuncType::FuncType(QObject *parent) : QObject(parent)
{m_functype= std::make_shared<FuncType>();m_clearTimer = new QTimer(this);connect(m_clearTimer, &QTimer::timeout, m_functype.get(), &FuncType::slotFunction);m_clearTimer->start(1000); // 1s
}