无法将QMap传递到SLOT [英] Can't pass QMap through to SLOT

查看:230
本文介绍了无法将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或某事吗?

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 >)));

编辑:我也尝试添加一个typeDef,但仍然得到相同的错误。

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) {

}


推荐答案

现在,我试图传递一个QMap< QString,QString>通过addMenu而不是一个QString,但我得到的错误:没有匹配的函数调用'QSignalMapper :: setMapping'。

"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'."

这不是一个常见的信号/槽或宏扩展问题,而是QSignalMapper的限制。你无法将QMap传递给setMapping,只能传递int,QString,QWidget *和QObject *。请参阅 QSignalMapper文档

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.

connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(addMenu(passedMapType));

这也不行,因为签名不兼容:QObject * vs. QMap。

That wouldn't work either, because the signatures are incompatible: QObject* vs. QMap.

将执行:在按钮的所有者中保持QMap< QObject *,PassedMapType>,在其中创建一个槽slotClicked(QObject *),连接到按钮的clicked()信号,然后从映射查找PassedMapType对象,使用传递的QObject *。
修改添加了一些伪代码来说明:

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*. added some pseudo code to illustrate it:

QMap<QWidget*, QMap<QString, QString> > map; //member of the class

//when creating the buttons:

for each button b:
    signalMapper->setMapping( b, b );
    map.insert( b, someMapForThisButton );
connect( signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(addMenuForButton(QWidget*)) );

//the slot:
void addMenuForButton(QWidget* w) {
     const QMap<QString, QString> m = map.value( w );
     create menu...
}

这篇关于无法将QMap传递到SLOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆