Qt将QAction连接到带参数的函数

km2*_*442 3 c++ qt qaction

在我的Qt 5.6程序中,我需要将QMenuClick(QAction)连接到函数并提供一些参数.我可以连接到没有参数的函数,它正在工作:

connect(MyAction, &QAction::triggered, function);
Run Code Online (Sandbox Code Playgroud)

但是,当我试图添加一些论点时:

connect(MyAction, &QAction::triggered, function(arguments));
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

C2664:"QMetaObject :: Connection QObject :: connect(const QObject*,const char*,const char ,Qt :: ConnectionType)const":无法从"void(__ thiscall QAction ::)(bool)"中获取2到"const char*"

我的示例功能:

void fuction(char x, char y, int z);
Run Code Online (Sandbox Code Playgroud)

谢谢你的任何建议.

Log*_*uff 6

function(arguments)是一个函数调用,您希望将函数绑定到参数并创建新的可调用对象,使用std::bind:

connect(MyAction, &QAction::triggered, std::bind(function, arguments));
Run Code Online (Sandbox Code Playgroud)

或者您可以使用lambda函数:

connect(MyAction, &QAction::triggered, [this]()
{
    function(arguments);
});
Run Code Online (Sandbox Code Playgroud)