要将 QCheckBox 的勾选白框置为灰色,可以使用 QPalette 将其不同状态下的颜色设置为所需的灰色。以下是一个示例代码:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *layout = new QVBoxLayout(&window);
QCheckBox *checkBox = new QCheckBox("Check Box");
// 获取当前的调色板
QPalette palette = checkBox->palette();
// 设置 CheckBox 不同状态下的颜色
palette.setColor(QPalette::Active, QPalette::Base, Qt::gray); // 正常激活状态下的背景颜色
palette.setColor(QPalette::Inactive, QPalette::Base, Qt::gray); // 非激活状态下的背景颜色
palette.setColor(QPalette::Disabled, QPalette::Base, Qt::lightGray); // 禁用状态下的背景颜色
checkBox->setPalette(palette);
layout->addWidget(checkBox);
window.show();
return app.exec();
}
在上述代码中,我们获取了 QCheckBox 的当前调色板,并使用 QPalette::setColor()
方法来设置其不同状态下的背景颜色(QPalette::Base
)为所需的灰色。
你可以根据需要调整其他状态和颜色组来实现不同效果。
内容由零声教学AI助手提供,问题来源于学员提问