Can't pass QMap through to SLOT

空扰寡人 提交于 2019-12-06 06:15:06

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

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

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:

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...
}

Use typedefs. My feeling is that the comma between the two QString template parameters is a problem in macro expansion.

As a nice work around you can use QObject. QObject has an embedded QMap inside. 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!