我正在使用
self.setStyleSheet("background-color: white")
更改PyQt5中QScrollArea的背景颜色,但这也会影响滚动条。更改区域背景颜色的正确方法是什么?
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QVBoxLayout, QScrollArea)
class TaskListWidget(QScrollArea):
def __init__(self):
super().__init__()
self.content = QWidget()
self.layout = QVBoxLayout(self.content)
for _ in range(20):
self.layout.addWidget(QLabel("task"))
self.setWidget(self.content)
self.setStyleSheet("background-color: white")
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.tasklist = TaskListWidget()
self.windowLayout = QVBoxLayout()
self.windowLayout.addWidget(self.tasklist)
self.setLayout(self.windowLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
一种可能的解决方案是将QScrollBar的背景色设置为None。
self.setStyleSheet(
"""
QWidget{ background-color: white }
QScrollBar{ background-color: none }
"""
)