对于 Qt 5 和 C++11 编译器,执行此类操作的惯用方法是为connect
:
connect(action1, &QAction::triggered, this, [this]{ onStepIncreased(1); });
connect(action5, &QAction::triggered, this, [this]{ onStepIncreased(5); });
connect(action10, &QAction::triggered, this, [this]{ onStepIncreased(10); });
connect(action25, &QAction::triggered, this, [this]{ onStepIncreased(25); });
connect(action50, &QAction::triggered, this, [this]{ onStepIncreased(50); });
第三个参数connect
名义上是可选的。它用于设置函子将在其中执行的线程上下文。当函子使用QObject
实例。如果函子使用多个QObject
例如,它们应该有一些共同的父对象来管理它们的生命周期,并且函子应该引用该父对象,或者应该确保对象的寿命比函子长。
在 Windows 上,这适用于 MSVC 2012 及更高版本。