我想从设置文件中动态添加一些QAction
:
_settings.beginGroup("openRecent");
QStringList recentList = _settings.childKeys();
foreach(QString recentFile, recentList)
{
QAction * action = new QAction(_settings.value(recentFile, "empty").toString(), this);
action->setObjectName(_settings.value(recentFile, "empty").toString());
connect(action, SIGNAL(triggered()), this, openFile(action->objectName()));
_recentFileButtons.append(action);
}
_settings.endGroup();
由于此行connect(action, SIGNAL(triggered()), this, openFile(action->objectName()));
如何将QAction连接到给定的功能(带参数)?
答案 0 :(得分:4)
你不能,不能直接
有两种选择:
使用sender()
获取发送QObject并使用
使用QSignalMapper将信号
添加一个参数signalMapper->setMapping(action, action->objectName());
connect(action, SIGNAL(triggered()), signalMapper, SLOT(map()));
并将signalMapper
与this
联系起来:
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(openFile(QString)));
答案 1 :(得分:1)
您不能以这种方式传递参数。我建议做以下事情:
connect(action, SIGNAL(triggered()), this, SLOT(openFile()));
在您的openFile()
广告位中,只需执行以下操作:
void MyClass::openFile()
{
QObject *obj = sender();
QString objName = obj->objectName();
[..]
}