在Properties-section,的文档中,有一个包含active-property的active。这让我相信,可能有类似于activeChanged-signal的东西,我可以连接到。
由于IMHO不明显的原因
QObject::connect(m_timer, &QTimer::activeChanged, this, &MyObject::mySlot);失败,声明activeChanged不是QTimer的成员。
基本上,我想做一些事情,当计时器最初启动(所以不是在重新启动)或最后停止。当信号activeChanged不存在时,有没有人知道:
试验在main.cpp中的应用
QTimer* tim = new QTimer;
QObject::connect(tim, &QTimer::activeChanged, qApp, [tim](){qDebug() << "Active changed" << tim->isActive(); });
tim->start(40000); // I want to get a signal
tim->start(100); // I don't want to get a signal
tim->stop(); // I want to get a signal发布于 2017-11-06 17:14:33
创建自己的timer类并封装QTimer:
class Timer : public QObject
{
Q_OBJECT
QTimer m_timer;
public:
Timer ()
{
connect(&m_timer, &QTimer::timeout, this, &Timer::timeout);
}
void start(int msec)
{
if (m_timer.isActive())
{
// Restart detected -> block signal
m_timer.blockSignals(true);
m_timer.start(msec);
m_timer.blockSignals(false);
}
else
{
m_timer.start(msec);
}
}
}因为类Timer拥有QTimer的全部控制和知识,所以您可以有任何明显的行为。
https://stackoverflow.com/questions/47141688
复制相似问题