我希望实现的目标是发出一个信号,但在实际发出之前延迟一段时间。
我的想法是这样的:
我的想法是这样的:
emitLater(QObject *obj, QEvent *event, int msDelay)
{
QTimer...
connect(timout to obj and event...
start timer with msDelay...
//maybe I might need a public timer of some sort but the idea is clear
}
所以这种方法在 QEvent
参数上失败了,因为信号无法转换为 QEvents
。
我尝试使用几种不同的 std::bind 调用方式,但是我无法让它正常工作:
//the call
std::function<void(void)> signal = std::bind(&CustomPushButton_Header::clicked, &uiWindow->btnHeader_left); //just a random button to connect a valid signal
emitLater(signal, 2000);
//the function
emitLater(std::function<void(void)>, int msDelay)
{
qDebug() << "hi";
}
我希望有一个适用于所有对象和信号的解决方案...也许只需要调用函数的部分。我认为延迟会很容易解决。
或者您有不同甚至更简单的方法。那将是很好的。
我查看了一些来源:
QEvent
,这是行不通的,因为当控制权返回事件循环时,QEvent
将被删除。一般来说,如果你有一个函数对象f
,想要在ms
毫秒后调用它,你可以使用QTimer::singleShot(ms, f)
。 - G.M.