• 4233阅读
  • 2回复

[提问]自定义控件怎么根据属性设置其子控件的样式啊 [复制链接]

上一主题 下一主题
离线hanqiu
 

只看楼主 倒序阅读 楼主  发表于: 2016-12-23
请问一下我自定义了一个控件类ItemEx,其有一个状态属性status,其中有这个控件类ItemEx中有一个QLabel的对象lab,请问一下,我怎么写SheetStyle,让status等于true和false的时候,lab为不同的样式呢?
比如status为真的时候,lab的背景为红色;false的时候,lab的背景为绿色
我想写在文本中一次性设置,不想在代码中写
离线hanqiu

只看该作者 1楼 发表于: 2016-12-26
求组
离线firebolt

只看该作者 2楼 发表于: 2016-12-26
Qt帮助里有,帮你粘贴上来了
There are many situations where we need to present a form that has mandatory fields. To indicate to the user that the field is mandatory, one effective (albeit esthetically dubious) solution is to use yellow as the background color for those fields. It turns out this is very easy to implement using Qt Style Sheets. First, we would use the following application-wide style sheet:

*[mandatoryField="true"] { background-color: yellow }
This means that every widget whose mandatoryField Qt property is set to true would have a yellow background.

Then, for each mandatory field widget, we would simply create a mandatoryField property on the fly and set it to true. For example:

QLineEdit *nameEdit = new QLineEdit(this);
nameEdit->setProperty("mandatoryField", true);

QLineEdit *emailEdit = new QLineEdit(this);
emailEdit->setProperty("mandatoryField", true);

QSpinBox *ageSpinBox = new QSpinBox(this);
ageSpinBox->setProperty("mandatoryField", true);
快速回复
限100 字节
 
上一个 下一个