Passing QLabel as a parameter in Qt Creator gives error
Question
I am trying to pass a QLabel as a parameter to another function that is supposed to change the style of the label:
main
{
...
setSeatColor(ui->lblPan2Seat5B, 2);
}
void setSeatColor(QLabel& lbl, int i)
{
if(i == 1)
{
lbl.setStyleSheet("QLabel { background-color : blue; color : white; }");
}
else if(i == 2)
{
lbl.setStyleSheet("QLabel { background-color : red; color : white; }");
}
else if(i == 3)
{
lbl.setStyleSheet("QLabel { background-color : green; color : white; }");
}
else
{
lbl.setStyleSheet("QLabel { background-color : rgb(240,240,240); color : back; }");
}
}
The error in the function:
"No matching function for call to setSeatColor(QLabel*&,int)"
Your help is much appreciated!
Solution
You pass a QLabel pointer to a function that takes a QLabel&, so replace this call:
setSeatColor(ui->lblPan2Seat5B, 2);
with this one (notice the dereference operator *)
setSeatColor(*ui->lblPan2Seat5B, 2);
//you can use extra parentheses for your colleagues, so that they can understand what are you doing there: setSeatColor( *(ui->lblPan2Seat5B), 2); - that way is a little clearer that you intend to dereference the lblPan2Seat5B not the ui that is dereferenced by the -> operator
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow