我试图在Qt 5.1和MSVC 2010中使用std::bind将QNetworkReply事件挂接到成员函数,同时将回复作为参数传递。在MSVC 2010上,直接将std::bind放入连接行失败,会出现大量模板错误,但使用std::函数将其拆分为两行是可行的。我愿意坚持一句话。让这一切发生的魔法咒语是什么?
void MyClass::doRequest ( )
{
..
QNetworkReply * reply = nam.get(...)
// next line fails on MSVC 2010
connect(reply, &QNetworkReply::finished, std::bind(&MyClass::onNetworkDone, this, reply));
// next two lines do work on MSVC 2010
std::function<void ()> a = std::bind<void()>(&MyClass::onNetworkDone, this, reply);
connect(reply, &QNetworkReply::finished, a);
}
void MyClass::onNetworkDone( QNetworkReply * reply )
{
..
}
您需要使用将绑定强制转换为函数指针
connect(reply, &QNetworkReply::finished, (void(*)()) std::bind(&MyClass::onNetworkDone, this, reply));
这回答了你的问题。然而,我不确定它是否会在运行时崩溃,原因在上面的评论中讨论过。