How to set stylesheet on widgets containing a given string?
-
Is it possible on qt designer to apply the stylesheet only on widgets containing a given string?
Something like[objectName*="test_"] { background-color: red; }It would set the bg red on any widget containing
test_on its object name.@Daniella
Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-typesInstead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.
-
@Daniella
Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-typesInstead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.
@JonB said in How to set stylesheet on widgets containing a given string?:
Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types
Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.
When i have a QWidget inside another QWidget the padding doesnt work, im doing something wrong?
QWidget[test="true"] { background-color: #3c3f45; padding-left: 32px; } -
@JonB said in How to set stylesheet on widgets containing a given string?:
Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types
Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.
When i have a QWidget inside another QWidget the padding doesnt work, im doing something wrong?
QWidget[test="true"] { background-color: #3c3f45; padding-left: 32px; }@Daniella
Things can go wrong on parenting the widgets into each other. The child may inherit its parent’s stylesheet. In that case the parent probably has the same background color. So the only noticeable difference is padding.It also matters if
testbecomes true before or after the setting the stylesheet. If it happens afterwards, you have to enforce manual recomputing.Whatever it is, setting the child’s stylesheet (again), should solve both issues.