无法将 QMap 传递到 SLOT
所以,这有效:
.h
public slots:
void addMenu(QString passedName);
signals:
void clicked(const QString &text);
.cpp
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, QString("passed_value"));
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(addMenu(QString)));
现在,我试图传递 QMap < QString, QString >到 addMenu 而不仅仅是 QString,但我收到错误:没有调用“QSignalMapper::setMapping”的匹配函数。我需要创建一个 typeDef 或其他东西吗?
.h
public slots:
void addMenu(QMap < QString, QString > map);
signals:
void clicked(const QMap < QString, QString > &map);
.cpp
//map is defined above
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, map);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(addMenu(QMap < QString, QString >)));
编辑:我也尝试添加 typeDef,但仍然遇到相同的错误。
.h.cpp
public:
typedef QMap < QString, QString > passedMapType;
public slots:
void addMenu(passedMapType map);
signals:
void clicked(passedMapType map);
passedMapType passedMap;
passedMap.insert(QString("key"), QString("value"));
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, passedMap);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(addMenu(passedMapType));
....
addMenu(passedMapType passedMap) {
}
So, this works:
.h
public slots:
void addMenu(QString passedName);
signals:
void clicked(const QString &text);
.cpp
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, QString("passed_value"));
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(addMenu(QString)));
Now, I am trying to pass a QMap < QString, QString > through to addMenu instead of just a QString, but I get the error: no matching function for call to 'QSignalMapper::setMapping'. Do I need to create a typeDef or something?
.h
public slots:
void addMenu(QMap < QString, QString > map);
signals:
void clicked(const QMap < QString, QString > &map);
.cpp
//map is defined above
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, map);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(addMenu(QMap < QString, QString >)));
EDIT: I also tried adding a typeDef, but still getting same error.
.h
public:
typedef QMap < QString, QString > passedMapType;
public slots:
void addMenu(passedMapType map);
signals:
void clicked(passedMapType map);
.cpp
passedMapType passedMap;
passedMap.insert(QString("key"), QString("value"));
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button, passedMap);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(addMenu(passedMapType));
....
addMenu(passedMapType passedMap) {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“现在,我尝试将 QMap < QString, QString > 传递给 addMenu 而不仅仅是 QString,但我收到错误:没有调用“QSignalMapper::setMapping”的匹配函数。”
这不是一般的信号/槽或宏扩展问题,而是 QSignalMapper 的限制。您不能将 QMap 传递给 setMapping,只能传递 int、QString、QWidget* 和 QObject*。请参阅 QSignalMapper 文档。
这也行不通,因为签名不兼容:QObject* 与 QMap。
我会做什么:持有 QMap在按钮的所有者中,创建一个插槽 slotClicked(QObject*) ,连接到按钮的 clicked() 信号,然后使用传递的 QObject* 从地图中查找 PassedMapType 对象。
编辑:添加了一些伪代码来说明它:
"Now, I am trying to pass a QMap < QString, QString > through to addMenu instead of just a QString, but I get the error: no matching function for call to 'QSignalMapper::setMapping'."
That's not a general signal/slot or macro expansion problem, but a limitation of QSignalMapper. You can't pass a QMap to setMapping, only int, QString, QWidget* and QObject*. See the QSignalMapper documentation.
That wouldn't work either, because the signatures are incompatible: QObject* vs. QMap.
What I would do: Hold a QMap<QObject*,PassedMapType> in the owner of the buttons, create a slot slotClicked(QObject*) there, connected to the clicked() signals of the buttons, and then look up the PassedMapType object from the map, using the passed QObject*.
Edit: added some pseudo code to illustrate it:
使用 typedef。我的感觉是两个QString模板参数之间的逗号是宏扩展的问题。
Use typedefs. My feeling is that the comma between the two QString template parameters is a problem in macro expansion.
作为一个很好的解决方法,您可以使用 QObject。 QObject 内部有一个嵌入的 QMap。事情是这样的:
As a nice work around you can use QObject. QObject has an embedded QMap inside. Here is how it goes:
这纯粹是猜测,但您可能需要使用指针类型?我从来没有很幸运地通过引用 &。
This is a pure guess, but you may need to use pointer types instead? I never had very much luck passing by reference with &.
信号和槽必须具有相同的参数类型(它是一个函数调用,某种意义上),所以这一行是错误的:
您可能需要重新考虑一下您的逻辑才能使其工作。
The signal and slot must have the same argument types (it's a function call, of sorts), so this line is wrong:
You might need to rethink your logic a bit to make that work.