我目前正在使用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;
}
虽然“标准”css的概念似乎可以应用于Qt样式表(QS),但这仅适用于语法和继承的基本方面:QS只实现css 2.1规范的部分,并且不允许布局管理(特定小部件中的边框、边距和填充除外),这是Qt的独家责任
这在尝试为滚动条设置“透明”背景时非常重要,因为Qt不允许CSS的“溢出”效果,因为滚动条通常在视口内容的外部绘制
为了达到类似的效果,解决方案是正确设置滚动区域的背景(在您的例子中是QGraphicsView)
注意,由于各种原因,在应用程序上设置通用样式表并不总是足够的,有时需要在滚动条(
self.view.horizontalScrollBar().setStyleSheet(...)
,self.view.verticalScrollBar().setStyleSheet(...)
)上显式地设置滚动条专用样式表然后,QGraphicscene中的小部件的问题就不同了。添加QGraphicsProxyWidget时,该widget被视为一个顶级窗口,因此它将根据应用程序调色板(看起来是白色的,但它是浅灰色的)拥有自己的背景。在这种情况下,解决方案与尝试显示“透明”窗口时相同:
相关问题 更多 >
编程相关推荐