dvilela
dvilela

Reputation: 1262

How to change the background color of a QScrollArea without affecting the scrollbar color?

I'm using

self.setStyleSheet("background-color: white")

to change the background color of a QScrollArea in PyQt5, but that also affects the scroll bar. What is the proper way of just changing the area background color?

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

Upvotes: 5

Views: 2359

Answers (1)

eyllanesc
eyllanesc

Reputation: 244212

One possible solution is to set the background-color of the QScrollBar to None.

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

Upvotes: 5

Related Questions