【问题标题】:How to change the background color of a QScrollArea without affecting the scrollbar color?如何在不影响滚动条颜色的情况下更改 QScrollArea 的背景颜色?
【发布时间】:2020-09-25 01:16:43
【问题描述】:

我正在使用

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_()

【问题讨论】:

  • @eyllanesc 完成,感谢您的建议。

标签: python pyqt pyqt5 qtstylesheets qscrollarea


【解决方案1】:

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

self.setStyleSheet(
    """
    QWidget{ background-color: white } 
    QScrollBar{ background-color: none } 
    """
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2012-07-05
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多