要将 QLabel 的字体颜色改为绿色,可以按照以下方式使用 QPalette:
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
QLabel label("Hello World");
QPalette palette = label.palette(); // 获取当前的调色板
// 使用 setBrush 方法将前景颜色(即字体颜色)设置为绿色
palette.setColor(QPalette::WindowText, QColor(0, 255, 0));
label.setPalette(palette);
window.setCentralWidget(&label);
window.show();
return app.exec();
}
在上述代码中,我们使用 palette.setColor(QPalette::WindowText, QColor(0, 255, 0))
将前景颜色(即字体颜色)设置为绿色。运行代码后,你会看到 QLabel 的字体颜色已经变成了绿色。
内容由零声教学AI助手提供,问题来源于学员提问