Passing an argument to a slot

后端 未结 5 2667
灰色年华
灰色年华 2020-11-22 16:15

I want to override mouseReleaseEvent with a bunch of QActions and QMenus...

connect(action1, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

connect(ac         


        
5条回答
  •  盖世英雄少女心
    2020-11-22 16:54

    With Qt 5 and a C++11 compiler, the idiomatic way to do such things is to give a functor to 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); });
    

    The third argument to connect is nominally optional. It is used to set up the thread context in which the functor will execute. It is always necessary when the functor uses a QObject instance. If the functor uses multiple QObject instances, they should have some common parent that manages their lifetime and the functor should refer to that parent, or it should be ensured that the objects will outlive the functor.

    On Windows, this works in MSVC2012 & newer.

提交回复
热议问题