无法将 QMap 传递到 SLOT

发布于 2024-10-03 18:57:30 字数 1687 浏览 2 评论 0原文

所以,这有效:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

_蜘蛛 2024-10-10 18:57:30

“现在,我尝试将 QMap < QString, QString > 传递给 addMenu 而不仅仅是 QString,但我收到错误:没有调用“QSignalMapper::setMapping”的匹配函数。”

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

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

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

我会做什么:持有 QMap在按钮的所有者中,创建一个插槽 slotClicked(QObject*) ,连接到按钮的 clicked() 信号,然后使用传递的 QObject* 从地图中查找 PassedMapType 对象。
编辑:添加了一些伪代码来说明它:

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

"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...
}
剪不断理还乱 2024-10-10 18:57:30

使用 typedef。我的感觉是两个QString模板参数之间的逗号是宏扩展的问题。

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

有深☉意 2024-10-10 18:57:30

作为一个很好的解决方法,您可以使用 QObject。 QObject 内部有一个嵌入的 QMap。事情是这样的:

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

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();
  }
飘过的浮云 2024-10-10 18:57:30

这纯粹是猜测,但您可能需要使用指针类型?我从来没有很幸运地通过引用 &。

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

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

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);
护你周全 2024-10-10 18:57:30

信号和槽必须具有相同的参数类型(它是一个函数调用,某种意义上),所以这一行是错误的:

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

您可能需要重新考虑一下您的逻辑才能使其工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文