查看完整版本: [-- 自定义控件怎么根据属性设置其子控件的样式啊 --]

QTCN开发网 -> Qt基础编程 -> 自定义控件怎么根据属性设置其子控件的样式啊 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

hanqiu 2016-12-23 20:42

自定义控件怎么根据属性设置其子控件的样式啊

请问一下我自定义了一个控件类ItemEx,其有一个状态属性status,其中有这个控件类ItemEx中有一个QLabel的对象lab,请问一下,我怎么写SheetStyle,让status等于true和false的时候,lab为不同的样式呢?
比如status为真的时候,lab的背景为红色;false的时候,lab的背景为绿色
我想写在文本中一次性设置,不想在代码中写

hanqiu 2016-12-26 14:24
求组

firebolt 2016-12-26 18:12
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);


查看完整版本: [-- 自定义控件怎么根据属性设置其子控件的样式啊 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled