什么是C++ 11中的lambda表达式?我什么时候用?他们解决了哪些问题在引入之前是不可能的?
一些示例和用例将是有用的.
可以将多个信号绑定到一个插槽(不是?).那么,有没有办法了解哪个小部件发送信号?我正在寻找像sender.NET中的事件参数之类的东西
我有一个QAction项目,我初始化如下:
QAction* action = foo->addAction(tr("Some Action"));
connect(action, SIGNAL(triggered()), this, SLOT(onSomeAction()));
Run Code Online (Sandbox Code Playgroud)
然后onSomeAction看起来像:
void MyClass::onSomeAction()
{
QAction* caller = qobject_cast<QAction*>(sender());
Q_ASSERT(caller != nullptr);
// do some stuff with caller
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,我得到了caller对象,我可以按预期使用它.然后我尝试使用C++ 11方式连接对象,如下所示:
connect(action, &QAction::triggered, [this]()
{
QAction* caller = qobject_cast<QAction*>(sender());
Q_ASSERT(caller != nullptr);
// do some stuff with caller
});
Run Code Online (Sandbox Code Playgroud)
但是caller始终为null,因此Q_ASSERT触发器.我怎样才能使用lambdas来获取发件人?