label = QtGui.QLabel("Hello World!") red_brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) # create a solid red brush label.setAutoFillBackground(True) # enable background fill label.setBackground(red_brush) # set the label background to the red brush
button = QtGui.QPushButton("Click me!") gradient = QtGui.QLinearGradient(0, 0, 0, button.height()) # create a vertical gradient gradient.setColorAt(0, QtGui.QColor(255, 255, 255)) # set the top color to white gradient.setColorAt(1, QtGui.QColor(255, 0, 0)) # set the bottom color to red gradient_brush = QtGui.QBrush(gradient) # create the gradient brush button.setStyleSheet("background-color: none;") # clear any default button styles button.setAutoFillBackground(True) # enable background fill button.setBackground(gradient_brush) # set the button background to the gradient brushThese examples demonstrate how the QBrush class can be used to fill graphical shapes with color or pattern, and how it can be used to set the background and foreground of GUI elements. The package library used in these examples is PyQt4.