简体   繁体   English

无法将QMap传递到SLOT

[英]Can't pass QMap through to SLOT

So, this works: 所以,这有效:

.h 。H

public slots:
void addMenu(QString passedName);

signals:
void clicked(const QString &text);

.cpp 的.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'. 现在,我试图将QMap <QString,QString>传递给addMenu而不是QString,但是我得到了错误:没有匹配的函数来调用'QSignalMapper :: setMapping'。 Do I need to create a typeDef or something? 我是否需要创建typeDef或其他内容?

.h 。H

public slots:
void addMenu(QMap < QString, QString > map);

signals:
void clicked(const QMap < QString, QString > &map);

.cpp 的.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. 编辑:我也尝试添加typeDef,但仍然出现相同的错误。

.h 。H

public:
typedef QMap < QString, QString > passedMapType;

public slots:
void addMenu(passedMapType map);

signals:
void clicked(passedMapType map);

.cpp 的.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) {

}

Use typedefs. 使用typedef。 My feeling is that the comma between the two QString template parameters is a problem in macro expansion. 我的感觉是两个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'." “现在,我试图将QMap <QString,QString>传递给addMenu而不是QString,但是我得到了错误:没有匹配的函数来调用'QSignalMapper :: setMapping'。”

That's not a general signal/slot or macro expansion problem, but a limitation of QSignalMapper. 这不是一般的信号/插槽或宏扩展问题,而是QSignalMapper的局限性。 You can't pass a QMap to setMapping, only int, QString, QWidget* and QObject*. 您不能将QMap传递给setMapping,只能传递int,QString,QWidget *和QObject *。 See the QSignalMapper documentation . 请参阅QSignalMapper文档

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

That wouldn't work either, because the signatures are incompatible: QObject* vs. QMap. 那也不行,因为签名是不兼容的: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*. 我要做什么:在按钮的所有者中持有QMap <QObject *,PassedMapType>,在其中创建一个插槽slotClicked(QObject *),连接到按钮的clicked()信号,然后从中查找PassedMapType对象。使用传递的QObject *映射。 Edit: 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...
}

As a nice work around you can use QObject. 作为一项不错的工作,您可以使用QObject。 QObject has an embedded QMap inside. QObject内部具有嵌入式QMap。 Here is how it goes: 这是怎么回事:

void sender() {
  ...
  ...
  QObject *data = new QObject(0);
  data->setProperty("Name","xxxx");
  data->setProperty("Address","yyyy");

  //You can also send QImage
  QImage image;
  ...
  data->setProperty("Image",image);

  emit dataReady(data);
 }

signals:
  void dataReady(QObject*);

public slots:
  void receiver(QObject *data) {
    QString Name = data->property("Name").toString();
    QString Address = data->property("Address").toString();
    QImage image = data->property("Image").value<QImage>();
    data->deleteLater();
  }

This is a pure guess, but you may need to use pointer types instead? 这纯粹是猜测,但您可能需要改用指针类型? I never had very much luck passing by reference with &. 我从来没有碰过&运气。

public slots:
void addMenu(QMap<QString, QString> *map);

signals:
void clicked(QMap<QString, QString> *map);

The signal and slot must have the same argument types (it's a function call, of sorts), so this line is wrong: 信号和插槽必须具有相同的参数类型(这是一个函数调用,类似),因此此行是错误的:

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

You might need to rethink your logic a bit to make that work. 您可能需要重新考虑一下自己的逻辑才能使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM