Search code examples
cssqt4transparencystylesheetpyqt4

How to activate alpha channel with Qt4 Style Sheets?


I've looked into the documentation for Qt Style Sheets and found out I could change the background colour of my widgets and even set transparency.

Here is code snippet that should---according to me---work:

application = QtGui.QApplication(sys.argv)
application.setStyleSheet(QtCore.QString('MainWindow {background-color: rgba(20, 0, 0, 75%)}'))

And indeed it works to the extent the background colour of the window is changed to the corresponding RGB values. However, as far as I can tell, the alpha value has no effect on my application.

EDIT: I realise the alpha channel is working now, only the application's background or canvas (I'm not sure how I should call it) is black. I need to make that transparent, not the main widget. How can I achieve this?

NB: I'm running on Ubuntu 11.04 with Gnome (but should that matter?).


Solution

  • I've found one answer elsewhere on SO but tags hadn't led met to it in the first place. So I'm going to answer my own question here:

    The Qt Style Sheet will provide transparency for widgets only, but those are painted on black canvas. In order to make this canvas transparent, an attribute of the main widget should be changed like so:

    self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
    

    This however provides a radical solution only: the entire window is transparent, but there is no way to set partial transparency like this. So I'm not entirely satisfied with this answer.