关于QStateMachine延迟函数的说明
Explanation about QStateMachine delay function
>我在程序中使用了这个函数:
void delay(QState * state1, int millisecond, QAbstractState * state2)
{
auto timer = new QTimer(state1);
timer->setSingleShot(true);
timer->setInterval(millisecond);
QObject::connect(state1, &QState::entered, timer, static_cast<void (QTimer::*)()>(&QTimer::start));
QObject::connect(state1, &QState::exited, timer, &QTimer::stop);
state1 -> addTransition(timer, SIGNAL(timeout()), state2);
}
我从一个示例中进行了复制粘贴,但我不理解这部分代码:
QObject::connect(state1,..., static_cast<void (QTimer::*)()>(&QTimer::start));
任何人都可以向我解释这个代码是什么?它在程序中是如何工作的?
附言。我试图用这个更改该代码,但它不起作用:
QTimer *timer = new QTimer(state1);
.
. //same code as before
.
QObject::connect(stato1,&QState::entered,timer,[&] {timer->start();} );
QObject::connect(stato1,&QState::exited, timer,[&] {timer->stop(); } );
stato1 -> addTransition(timer,SIGNAL(timeout()),stato2);
有两个QTimer::start
插槽,一个没有参数,一个有int msec
参数。要使用新的连接语法连接到正确的插槽,必须使用 static_cast
指定插槽类型。
所以在这一行中:
QObject::connect(state1, &QState::entered, timer, static_cast<void (QTimer::*)()>(&QTimer::start));
您连接到QTimer::start
不需要参数的插槽。
如果你有一个带有int
参数的信号,并且你想连接到QTimer::start(int msec)
插槽,你可以这样做:
connect(this, &MyClass::mySignal, timer, static_cast<void (QTimer::*)(int)>(&QTimer::start));
您可以在此处阅读有关使用重载信号/插槽和新连接语法的更多信息。
您还可以使用qOverload
来消除丑陋static_cast
的需求。
在使用 lambda 表达式的代码段中,您可以通过引用捕获timer
。您应该改为按值捕获它:
QObject::connect(stato1, &QState::entered, timer, [=]{timer->start();});
相关文章:
- 当使用通配符和null指针调用函数时,对输出的说明
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 随机数生成函数说明
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 使用英特尔内部函数 (AVX) 中的混合说明
- 如何在不在基类中的派生类中访问 STL 类的成员函数?(正文详细说明)
- unique_ptr赋值和复制构造函数说明
- 说明通过指针访问非静态类成员函数
- 我们应该在带有 char *func(char *string) 的函数中返回什么?需要简要说明这与 c 中的 char
- C++ 函数中"Unreachable code"的说明
- 反向链表的递归函数(代码段说明)
- 需要说明简单的C 程序中的编译误差,以演示超载函数
- 关于QStateMachine延迟函数的说明
- 模板化函数指针说明
- C /参考操作员和指针中的插座连接()函数说明
- 函数指针说明
- C++03 12.4/12对通过指针显式调用基类析构函数有何说明
- C++图形函数内存泄漏(valgrind在线上没有具体说明)
- 转换构造函数说明
- 复制构造函数说明