如何设置QWidget的背景颜色

本文档介绍了如何使用QPalette和StyleSheet设置QWidget及其子类的背景颜色。通过设置autoFillBackground属性,定义QPalette对象并调整其背景,可以改变背景颜色。而使用Qt的StyleSheet时,需要注意默认会作用于所有子控件。通过三种不同方式限制StyleSheet的作用范围,避免影响到不希望更改的控件,如QTextEdit的例子。详细展示了如何只影响特定控件的背景,确保样式不会泄露。
摘要由CSDN通过智能技术生成

Introduction

QWidget is the base class of all user interface objects which means that the same approaches for changing the background color can be used with them too.

Using the Palette

The first example demonstrates how to change the background color using QPalette

首先设置autoFillBackground属性为真

然后定义一个QPalette对象

设置QPalette对象的背景属性(颜色或图片)

最后设置QWidget对象的Palette

 
  
m_pMyWidget = new QWidget(this);
m_pMyWidget->setGeometry(0,0,300,100);
QPalette
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值