ZBLOG

QPalette改变QLabel的颜色为绿色

要将 QLabel 的背景颜色改为绿色,可以按照以下方式使用 QPalette:

  1. #include <QApplication>
  2. #include <QMainWindow>
  3. #include <QLabel>
  4. #include <QPalette>
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication app(argc, argv);
  8. QMainWindow window;
  9. QLabel label("Hello World");
  10. QPalette palette = label.palette(); // 获取当前的调色板
  11. // 使用 setBrush 方法将背景颜色设置为绿色
  12. palette.setColor(QPalette::Background, QColor(0, 255, 0));
  13. label.setAutoFillBackground(true); // 设置自动填充背景
  14. label.setPalette(palette);
  15. window.setCentralWidget(&label);
  16. window.show();
  17. return app.exec();
  18. }

在上述代码中,我们将 palette.setColor(QPalette::Background, QColor(0, 255, 0)) 中的 RGB 值设为 (0, 255, 0),即红色值为 0、绿色值为 255、蓝色值为 0,以实现绿色背景。运行代码后,你会看到 QLabel 的背景颜色已经变成了绿色。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=12165

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?