不兼容的发送方/接收方参数

Incompatible Sender/ Receiver arguments

我想写一个Signalmapper。它应该将一个按钮连接到一个函数。这个函数应该获取被点击的 Qpushbutton 本身。

   void StrategicWidget::addTransporterImages(int i,unsigned int  numberTransporters)
      {
      Planet* p = PLANETS_VEC[i];
      QString qBasePath = QString::fromStdString(basePath);
       for(int j=0; j<numberTransporters; j++)
      {
         QPushButton* button = new QPushButton("", this);
         button->setGeometry(QRect(QPoint(p->getXCoord() + 30 + 15*j, p->getYCoord()+ 30 + 15*j), QSize(53, 53)));
         QString css = "background-image: url(" + qBasePath + "/Transporter.png)";
         button->setStyleSheet(css);  // sets button background image

      /*
       *SignalMapper zum festlegen welcher transporter der zu bewegende Transporter ist.
       *
       */
      QSignalMapper* transporterMapper = new QSignalMapper (button);
      // connecting button signal with signal mapper
       connect (button, SIGNAL(clicked()), transporterMapper, SLOT(map())) ;

      // giving parameter to the buttons
      transporterMapper->setMapping (button, button); 
      // map the signalMapper mapped signal to the transporterIconClicked-Slot of this widget
      connect (transporterMapper, SIGNAL(mapped(int)), this, SLOT(transporterIconClicked(QPushButton*)));

      }
    }

为什么我会在运行时收到此错误消息?

QObject::connect: Incompatible sender/receiver arguments QSignalMapper::mapped(int) --> asteroids::StrategicWidget::transporterIconClicked(QPushButton*)

我知道其他一些帖子也包含这个问题,但解决方案对我不起作用。

好吧,您正在将带有 int 参数的信号连接到采用 QPushButton* 的插槽。你期待什么?不行。

您需要做的是将 mapped(QWidget*) 信号连接到占用 QWidget* 的插槽,然后确保您确实获得了一个按钮:

void addTransporterImages(int i, unsigned int numberTransporters) {
  ...
  connect (transporterMapper, SIGNAL(mapped(QWidget*)), 
    SLOT(transporterIconClicked(QWidget*)));
  ...
}

Q_SLOT void transporterIconClicked(QWidget * widget) {
  auto button = qobject_cast<QPushButton*>(widget);
  if (!button) return;
  ....
}