python - ScrollBar QStyleSheet 显示时带有背景 它应该是透明的

标签 python pyside2 qtstylesheets qscrollbar

我正在尝试设置 QtTableWidget 滚动条的样式,以便“滚动部分”不在滚动条上的部分是透明的,因此滚动条将具有背景和滚动部分的颜色(您可以在其中设置滚动条)放置鼠标)是唯一颜色与背景不同的部分。

我尝试将其作为样式表:

QTableWidget {
    background-color:rgb(57, 57, 57);
    color:rgb(255,255,255);
    border-radius:3px;
}

QScrollBar:vertical {
    border: none;
    background: rgb(45, 45, 68);
    width: 14px;
    margin: 15px 0 15px 0;
    border-radius: 0px;
 }

/*  HANDLE BAR VERTICAL */
QScrollBar::handle:vertical {   
    background-color: rgb(80, 80, 122);
    min-height: 30px;
    border-radius: 7px;
}

但是当显示窗口时,我想要透明的部分被填充为白色填充背景,如下所示:

ScrollBar

我不确定样式表中出现此问题的原因。

这是重现窗口的代码:

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.tableWidget = QTableWidget(self.centralwidget)
        if (self.tableWidget.rowCount() < 100):
            self.tableWidget.setRowCount(100)
        self.tableWidget.setObjectName(u"tableWidget")
        self.tableWidget.setGeometry(QRect(30, 30, 721, 461))
        self.tableWidget.setStyleSheet(u"QTableWidget {\n"
"   background-color:rgb(57, 57, 57);\n"
"   color:rgb(255,255,255);\n"
"   border-radius:3px;\n"
"}\n"
"\n"
"QScrollBar:vertical {\n"
"   border: none;\n"
"    background: rgb(45, 45, 68);\n"
"    width: 14px;\n"
"    margin: 15px 0 15px 0;\n"
"   border-radius: 0px;\n"
" }\n"
"\n"
"/*  HANDLE BAR VERTICAL */\n"
"QScrollBar::handle:vertical {  \n"
"   background-color: rgb(80, 80, 122);\n"
"   min-height: 30px;\n"
"   border-radius: 7px;\n"
"}\n"
"QScrollBar::handle:vertical:hover{ \n"
"   background-color: rgb(255, 0, 127);\n"
"}\n"
"QScrollBar::handle:vertical:pressed {  \n"
"   background-color: rgb(185, 0, 92);\n"
"}")
        self.tableWidget.setRowCount(100)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 26))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)


        QMetaObject.connectSlotsByName(MainWindow)


class MainWindow(QMainWindow):
    
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

最佳答案

尝试一下:

'''
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *
'''
from PyQt5.Qt import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.tableWidget = QTableWidget(self.centralwidget)
        if (self.tableWidget.rowCount() < 100):
            self.tableWidget.setRowCount(100)
        self.tableWidget.setObjectName(u"tableWidget")
        self.tableWidget.setGeometry(QRect(30, 30, 721, 461))
        
#        self.tableWidget.setStyleSheet('''
        MainWindow.setStyleSheet('''
            QTableWidget {
                background-color:rgb(57, 57, 57);
                color:rgb(255, 255, 255);
                border-radius:3px;
            }
                        
            /* VERTICAL SCROLLBAR */
            QScrollBar:vertical {
                border: none;
                background: rgb(45, 45, 68);
                width: 14px;
                margin: 15px 0 15px 0;
                border-radius: 0px;
            }
            /*  HANDLE BAR VERTICAL */
            QScrollBar::handle:vertical {
                background-color: rgb(80, 80, 122);
                min-height: 30px;
                border-radius: 7px;
            }
            QScrollBar::handle:vertical:hover {
                background-color: rgb(255, 0, 127);
            }
            QScrollBar::handle:vertical:pressed {
                background-color: rgb(185, 0, 92);
            }
            /* BTN TOP - SCROLLBAR */
            QScrollBar::sub-line:vertical {
                border: none;
                background-color: rgb(59, 59, 90);
                height: 15px;
                border-top-left-radius: 7px;
                border-top-right-radius: 7px;
                subcontrol-position: top;
                subcontrol-origin: margin;
            }
            QScrollBar::sub-line:vertical:hover {
                background-color: rgb(255, 0, 127);
            }
            QScrollBar::sub-line:vertical:pressed {
                background-color: rgb(185, 0, 92);
            }

            /* BTN BOTTOM - SCROLLBAR */
            QScrollBar::add-line:vertical {
                border: none;
                background-color: rgb(59, 59, 90);
                height: 15px;
                border-bottom-left-radius: 7px;
                border-bottom-right-radius: 7px;
                subcontrol-position: bottom;
                subcontrol-origin: margin;
            }
            QScrollBar::add-line:vertical:hover {
                background-color: rgb(255, 0, 127);
            }
            QScrollBar::add-line:vertical:pressed { 
                background-color: rgb(185, 0, 92);
            }


/* RESET ARROW  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv       */
            QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {
                background: none;
            }
            QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
                background: none;
            }
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^       */
       
        ''')
        self.tableWidget.setRowCount(100)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 26))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        QMetaObject.connectSlotsByName(MainWindow)


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

enter image description here

关于python - ScrollBar QStyleSheet 显示时带有背景 它应该是透明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67442347/

相关文章:

python - 为什么在 Windows 终端中用 Python 进行彩色打印不起作用?

python - 使用样式表更改paintEvent颜色?

python - 重新实现QAbstractTableModel后TableView没有数据显示在表格中

python - QListWidget 项目突出显示的项目有不同的颜色

c++ - 如何删除Qtreewidget中的虚线分支

python - 如何在 pyqt4 中使用 qss?

python - Pandas:根据数据框中的组合填充缺失值

python - 计算一个元素在 python 列表中连续出现 n 次的次数

qt - 无法从外部 QSS 样式表文件将边框设置为 QDockWidget

python - 文件读/写意外行为