简体   繁体   English

Qt5 中子部件的透明度

[英]Transparency for the child widget in Qt5

I use Nvidia Jetson Nano with Linux for Tegra (Ubuntu 18, X11 window system), Python 3.6 and PyQt5.我将 Nvidia Jetson Nano 与 Linux 一起用于 Tegra(Ubuntu 18、X11 窗口系统)、Python 3.6 和 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.即使 gstreamer 视频流显示在主小部件中,透明度也能正常工作。

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_())

[ [在此处输入图片说明 在此处输入图片说明

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_())

在此处输入图片说明 在此处输入图片说明

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).此外,如果我为子小部件设置属性 self.setAttribute(Qt.WA_TranslucentBackground),主窗口中会出现一个“洞”(如果视频正在此窗口中播放)。

在此处输入图片说明

How can transparency be set for a child widget?如何为子小部件设置透明度? Thanks in advance for your answers!提前感谢您的回答!

As the name suggests, the windowOpacity refers to the opacity of the window .顾名思义, windowOpacity指的是window的不透明度。

If you want to set the opacity of a child widget, you need to use QGraphicsOpacityEffect :如果要设置子部件的不透明度,则需要使用QGraphicsOpacityEffect

class MenuWidget(QWidget):
    def __init__(self, parent): 
        QWidget.__init__(self, parent)
        # ...
        self.setGraphicsEffect(QGraphicsOpacityEffect(opacity=.5))

As I understand it, the transparency (background transparency) of the widget can only be created using the desktop compositor.据我了解,小部件的透明度(背景透明度)只能使用桌面合成器创建。 Qt cannot use desktop compositor to render child widgets. Qt 不能使用桌面合成器来渲染子部件。 The only solution is to use independent windows / widgets.唯一的解决方案是使用独立的窗口/小部件。 The only solution is to use independent windows/widgets.唯一的解决方案是使用独立的窗口/小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM