Reputation: 17
I want to write a Signalmapper. It should connect a button to a function. This function should get the clicked Qpushbutton itself.
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*)));
}
}
Why do I get this error message at runtime?
QObject::connect: Incompatible sender/receiver arguments QSignalMapper::mapped(int) --> asteroids::StrategicWidget::transporterIconClicked(QPushButton*)
I know that some other posts include this problem too, but the solutions didn't work for me.
Upvotes: 0
Views: 2760
Reputation: 98425
Well, you're connecting a signal with an int
argument to a slot taking a QPushButton*
. What did you expect? It can't work.
What you need to do instead is to connect the mapped(QWidget*)
signal to a slot taking QWidget*
, and then ensure that you indeed get a button:
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;
....
}
Upvotes: 0