Download the python file, drop it in your project folder, and make sure to add the import statement in your main code.
from QtStyleHelper import Styles
PySide's QtGui code is great. It lets you style widgets using css-like style sheets. Look here for the Style Sheet Reference page
But managing the style sheets is a bit of pain. You could make a big long string yourself, like
myStyle = 'QLabel{ background-color: red; }'
But that'd get annoying really fast if you had a lot of styling to do. This utility allows you to do stuff more like this
s = Styles()
labels = (s['QLabel']
.set('background-color', 'red')
)
Its more code, yes, but it is way easier to extend later. For example, if you wanted to implement some of the given examples, writing the string version would be a pain, but writing it with this utility class is a breeze. Take the pushbutton style example...
QPushButton#evilButton {
background-color: red;
border-style: outset;
border-width: 2px;
border-color: beige;
}
To implement that in the utility, it looks like
s = Styles()
evilButton = (s['QPushButton#evilButton']
.set('background-color', 'red')
.set('border-style', 'outset')
.set('border-width', '2px')
.set('border-color', 'beige')
)
I'm not going to write out the string assembly version of that, because it'd be nasty to look at. Anyway, to apply the style, you just call the setStyleSheet
function with str(s)
.
evilWidget = QPushButton()
evilWidget.setObjectName('evilButton')
s = Styles()
# assign evilButton like in the example above...
evilWidget.setStyleSheet(str(s)) #important bit that sets the style.
Thats pretty much it. The rest is up to you! Quick notes about it
-
To select an object, use
s['selection']
where selection is any valid Qt Style Sheet selection. You can see those in the documentation. -
You can chain
set
methods together like I did in the examples, but you can also call them one after another.s = Styles() labels = s['QLabel'] labels.set('background-color', 'red') labels.set('color', 'green')
-
Generally you would make a
Styles
for the entire QFrame or window or something, and then apply the style to that.