qt 给对象函数一个 ui 组件

qt give an ui component to an object function

我想创建一个对象函数来改变某个按钮的颜色。我想给它一个 ui-> 按钮。

所以我会用这样的方式调用函数:

changeButtonColor(ui->pushbutton);

函数需要怎么写?

void MainWindow::changeButtonColor(ui->pushbutton)
{
}

如果您想修改函数中的按钮,您有两种选择将其作为参数传递给函数:作为指针或作为引用。在每种情况下,函数签名(原型)将是:

指针参数:

void MainWindow::changeButtonColor(QPushButton * button);

参考参数:

void MainWindow::changeButtonColor(QPushButton & button);

在每种情况下,对函数的调用都是:

指针参数:

changeButtonColor(ui->pushbutton);

参考参数:

changeButtonColor(*ui->pushbutton);

更多信息:difference between a pointer and reference parameter?