Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

Transparency for the child widget in Qt5



  • Hello!

    I use Nvidia Jetson Nano with Linux for Tegra (Ubuntu 18, X11 window system), Python 3.6 and PyQt5. I want to place a transparent widget (or with a transparent background) over the main widget.

    When these widgets are created as independent everything is displayed correctly. Transparency works even if the gstreamer video stream is displayed in the main widget.

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    class MainWindow(QWidget):   
        def __init__(self):
            QWidget.__init__(self)
            self.setGeometry(50,50,320,240)
            self.setWindowTitle("Main Window")
            self.setStyleSheet("background-color:yellow;")
            self.label = QLabel(self)
            self.label.setText("Main Widget")
            self.menu = MenuWidget()
        
    class MenuWidget(QWidget):
        def __init__(self): 
            QWidget.__init__(self)
            self.setWindowFlags(Qt.Tool)
            self.setGeometry(100,100,100,50)
            self.setWindowFlags(Qt.FramelessWindowHint)
            self.setStyleSheet("background-color:gray;")
            self.setWindowOpacity(0.5)
            self.label = QLabel(self)
            self.label.setText("Menu Widget")
    
    app = QApplication([])
    window = MainWindow()
    window.show()
    window.menu.show()
    sys.exit(app.exec_())
    

    1_1.jpg
    2_2.jpg

    When I try to create a widget as a child of the main widget, transparency does not work. If a video is displayed, a "hole" appears in the main widget window.

    class MainWindow(QWidget):   
        def __init__(self):
            QWidget.__init__(self)
            self.setGeometry(50,50,320,240)
            self.setWindowTitle("Main Window")
            self.setStyleSheet("background-color:yellow;")
            self.label = QLabel(self)
            self.label.setText("Main Widget")
            self.menu = MenuWidget(parent=self)
            print('main window created')
        
    class MenuWidget(QWidget):
        def __init__(self, parent): 
            QWidget.__init__(self, parent)
            self.setWindowFlags(Qt.Tool)
            self.setGeometry(100,100,100,50)
            self.setWindowFlags(Qt.FramelessWindowHint)
            self.setStyleSheet("background-color:gray;")
            self.setWindowOpacity(0.5)
            self.label = QLabel(self)
            self.label.setText("Menu Widget")
    
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
    

    3_3.jpg
    4_4.jpg

    Also, if I set the attribute self.setAttribute(Qt.WA_TranslucentBackground) for the child widget, a "hole" appears in the main window (if video is playing in this window).

    5_5.jpg

    How can transparency be set for a child widget? Thanks in advance for your answers!


Log in to reply