Using Qt to create the OS X Yosemite Translucency effect

1.2k Views Asked by At

We're trying to make a QMainWindow with the Mac OSX Yosemite translucency effect. We're using PyQt, but the problem is a Qt issue. With what we've tried so far, it's always either fully transparent or fully opaque (like a normal window). If we turn on Qt.WA_TranslucentBackground, the window background becomes 100% completely transparent.
Additionally, the QGraphicsView we're displaying on it then leaves trails behind when you scroll. The mouse input also "passes through" the transparent parts - clicking on a transparent portion of the graphics view will register as a click on the window behind it. Setting a stylesheet with any custom background color then has no effect. If we turn it off, the window remains opaque. Then we can change the background color using a style sheet, but it's still opaque.
Turing WA_FramelessWindowHint on and off doesn't seem to fix anything, either. Nor does setAutoFillBackground(). Do you know how to make a window with the Yosemite translucency effect?

Here's a sample Python program to test this: -

# Attempt at Mac OSX Translucency (example code)

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt

class ExampleMainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.testWidget = QtWidgets.QLabel('Test label, which should be transparent')

        # Make sure the testWidget is transparent
        self.testWidget.setStyleSheet('background: transparent')
        self.testWidget.setAttribute(Qt.WA_TranslucentBackground, True)
        self.testWidget.setAutoFillBackground(True)

        self.setStyleSheet('background: rgba(255, 255, 255, 0.8)')
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setAutoFillBackground(True)
        #self.setWindowFlags(Qt.FramelessWindowHint) # Doesn't seem to help

        self.setCentralWidget(self.testWidget)

def main():
    global app, exWindow
    app = QtWidgets.QApplication(sys.argv)

    exWindow = ExampleMainWindow()
    exWindow.show()
    exitcodesys = app.exec_()
    app.deleteLater()
    sys.exit(exitcodesys)

if __name__ == '__main__': main()
1

There are 1 best solutions below

5
On

Your stylesheet isn't valid. Qt's rgba expects an integer in range 0-255 or a percentage for the values only. So use 80% instead of 0.8. Along with WA_TranslucentBackground that should get you going.

And please post your code in the question, as your pastes expire in a few days and this question will then be unusable.

Here is code that is working for me; I see my other applications' windows underneath. It's C++. I'm using Qt4 rather than 5; perhaps this matters.

#include <QMainWindow>
#include <QApplication>
#include <QLabel>

class MyMain : public QMainWindow
{
    Q_OBJECT

public:
    MyMain(QWidget* parent = 0);

};

MyMain::MyMain(QWidget* parent)
    : QMainWindow(parent)
{
    setAttribute(Qt::WA_TranslucentBackground, true);
    setStyleSheet("background: rgba(0,0,0,80%);");

    QLabel* foo = new QLabel();
    foo->setText("hello");
    foo->setStyleSheet("color: white;");
    setCentralWidget(foo);
}

#include "main.moc"

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    MyMain m;
    m.show();
    return app.exec();
}