如何在不影响滚动条颜色的情况下更改QScrollArea的背景颜色?

问题描述 投票:2回答:1

我正在使用

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_()
python pyqt pyqt5 qtstylesheets qscrollarea
1个回答
2
投票

一种可能的解决方案是将QScrollBar的背景色设置为None。

self.setStyleSheet(
    """
    QWidget{ background-color: white } 
    QScrollBar{ background-color: none } 
    """
)
© www.soinside.com 2019 - 2024. All rights reserved.