-2

I want to create an object function that changes the color of a certain pushbutton. I would like to give an ui->pushbutton to it.

So I would be calling the function with something like this:

changeButtonColor(ui->pushbutton);

How do I need to write the function?

void MainWindow::changeButtonColor(ui->pushbutton)
{
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

If you want to modify the pushbutton in the function you have two options to pass it as an argument to a function: as a pointer, or as a reference. In each case, the function signature (prototype) would be:

Pointer parameter:

void MainWindow::changeButtonColor(QPushButton * button);

Reference parameter:

void MainWindow::changeButtonColor(QPushButton & button);

In each case, a call to the function would be:

Pointer argument:

changeButtonColor(ui->pushbutton);

Reference argument:

changeButtonColor(*ui->pushbutton);

More information: difference between a pointer and reference parameter?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Former contributor
  • 2,466
  • 2
  • 10
  • 15