I've got a QWidget that contains several QLineEdits. When I tell the parent QWidget to change its background color dynamically, I'd like the children (i.e. QLineEdits) to inherit this modification.
Is there an easy (read: one function call) to do th开发者_如何转开发is?
If nothing pops up, I think I'll just loop through the children of the QWidget, but when doing this properly I expect to end up with a recursive function with a lot of overhead, that's why I'm asking.
EDITs in Bold face.
Generally speaking you don't need to worry about "overhead" in dialogs. Unless you're doing some sort of massive draw operation, UI applications simply don't need a lot of optimizations. Digging down to all children and changing their background is a relatively fast operation compared to the Qt system itself actually doing the change.
That said, I'd assume there is a way to get what you want but I don't know what it is. My bet is that it will do exactly what you would anyway.
How are you going about telling it to "change color" btw? Qt doesn't seem to have operations to do that. You can assign a background role or change the pallete. As to the latter:
http://doc.qt.nokia.com/4.7/qwidget.html#palette-prop
If you set the background color using a style sheet assigned to that widget and don't specify any selectors in the CSS, all child widgets will inherit any properties that apply to them.
I found it useful to use a selector that allowed me to target specific widgets for a certain style.
QWidget[objectName|="special_color"]
{
color: rgb(255, 255, 255);
}
If I used this in a style sheet assigned to a container widget, it would apply the specified color to any child widgets whose name started with "special_color
" like "special_colorEditBox1
" no matter how they are nested or contained.
精彩评论