簡體   English   中英

PyQt5 - setStyleSheet 透明/alpha 不起作用?

[英]PyQt5 - setStyleSheet transparent/alpha not working?

我目前正在使用 PyQt5 編寫一些程序,我想在 UI 中設置滾動條的樣式

QScrollBar:horizontal {
    border: 0px;
    background-color: transparent;
    color: yellow;
    height: 15px;
    margin: 0px 20px 0px 20px;
}

QScrollBar::handle:horizontal {
    background: rgb(100,100,104);
    min-width: 20px;
}

QScrollBar::add-line:horizontal {
    border: 0px;
    background-color: transparent;
    color: yellow;
    width: 20px;
    subcontrol-position: right;
    subcontrol-origin: margin;
}

QScrollBar::sub-line:horizontal {
    border: 0px;
    background-color: transparent;
    color: yellow;
    subcontrol-position: left;
    subcontrol-origin: margin;
}

但是當我運行程序時它並沒有真正出現。

在此處輸入圖像描述

我還嘗試在 UI 中設置其他項目的樣式,這次是通過添加內聯樣式表。

self.content = QTextEdit()
self.content.setStyleSheet("QTextEdit {border: 5px solid transparent}")

但這顯示了:

在此處輸入圖像描述

那個邊框不應該是透明的嗎? 我嘗試過設計其他項目的樣式,但似乎無法使透明樣式起作用。 我也嘗試過使用rgba(0,0,0,0)而不是透明的,我得到了相同的結果。 我在這里缺少什么嗎?

編輯:這是@musicamante 要求的 MRE

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.stylesheet_filename = "qss/styles.qss"
        self.loadStyleSheet(self.stylesheet_filename)

        self.setGeometry(200,200,800,600)

        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        
        self.scene = MainScene()
        self.view = QGraphicsView()

        self.view.setScene(self.scene)
        self.layout.addWidget(self.view)

        self.show()

    def loadStyleSheet(self, filename):
        print('Style loading: ', filename)
        file = QFile(filename)
        file.open(QFile.ReadOnly | QFile.Text)
        stylesheet = file.readAll()
        QApplication.instance().setStyleSheet(str(stylesheet, encoding='utf-8'))

class MainScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setSceneRect(-1000,-1000,2000,2000)
        self.setBackgroundBrush(QColor("#252629"))
        self.card = Card()
        self.addItem(self.card)


class Card(QGraphicsItem):
    def __init__(self, parent = None):
        super().__init__(parent)    

        self.setFlag(QGraphicsItem.ItemIsSelectable)
        self.setFlag(QGraphicsItem.ItemIsMovable)  
        
        self.content = Content()

        self.grContent = QGraphicsProxyWidget(self)
        self.content.setGeometry(10,15, 280, 150)
        self.grContent.setWidget(self.content)

    def boundingRect(self):
        return QRectF(0,0,300,180).normalized()

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        #background
        path_background = QPainterPath()
        path_background.addRect(0,0,300,180)
        painter.setPen(Qt.NoPen)
        painter.setBrush(QBrush(QColor("#998090")))
        painter.drawPath(path_background.simplified())


class Content(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        self.content = QTextEdit('I want the background of this QTextEdit and the scrollbar tracks to be transparent, not white.')
        self.content.setStyleSheet("QTextEdit {border: 0px; background-color: transparent}")
        
        self.layout.addWidget(self.content)
        

if __name__ == "__main__":
    app = QApplication(sys.argv)

    mainWindow = MainWindow()
    
    sys.exit(app.exec_())

編輯2:我忘了粘貼qss代碼:

QScrollBar:horizontal {
    border: 0px;
    background: rgba(0,0,0,0);
    height: 15px;
    margin: 0px 20px 0 20px;
}
QScrollBar::handle:horizontal {
    background: red;
    min-width: 20px;
}
QScrollBar::add-line:horizontal {
    border: 0px;
    background: rgba(0,0,0,0);
    width: 20px;
    subcontrol-position: right;
    subcontrol-origin: margin;
}

QScrollBar::sub-line:horizontal {
    border: 0px;
    background: rgba(0,0,0,0);
    width: 20px;
    subcontrol-position: left;
    subcontrol-origin: margin;
}

While it might seem that the concepts of "standard" css can be applied to Qt StyleSheets (QSS), that is only true for the basic aspects of syntax and inheritance: QSS only implements some of the CSS 2.1 specification, and does not allow layout管理(特定小部件內的邊框、邊距和填充除外),這是 Qt 的專屬責任。

這在嘗試為滾動條設置“透明”背景時非常重要,因為 Qt 不允許 CSS 的“溢出”效果,因為滾動條通常繪制在視口內容之外

為了達到類似的效果,解決方案是正確設置滾動區域的背景(在您的情況下為 QGraphicsView)。

QGraphicsView { 
    background: #252629; 
}

請注意,由於各種原因,在應用程序上設置通用樣式表並不總是足夠的,有時需要在滾動條顯式設置滾動條專用樣式表( self.view.horizontalScrollBar().setStyleSheet(...) , self.view.verticalScrollBar().setStyleSheet(...) )。

那么,QGraphicsScene 中的小部件的問題就不同了。 添加 QGraphicsProxyWidget 時,該小部件被視為頂級window,因此它將有自己的背景,基於應用程序調色板(看起來是白色的,但它是淺灰色的)。 在這種情況下,解決方案與嘗試顯示“透明”window 時相同:

class Content(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setAttribute(Qt.WA_TranslucentBackground)
        # ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM