To be clear, this has nothing to do with the items in a list view, but the default background color itself. I'd like the widget to effectively not be noticeable itself.
According to the docs setting the background role to NoRole
will achieve my desired effect, but does not. I assume I am misunderstanding the documentation.
I realize I can change this with setStyleSheet()
however, I'd prefer to avoid style sheets if possible.
The following is a small example app. The desired effect is that you cannot see the list view view. When I start adding items, I want to style them specifically.
from PyQt6.QtGui import QPalette
from PyQt6.QtWidgets import QFrame, QListView, QWidget
class ListView(QListView):
''' Custom list view widget. '''
def __init__(self, parent: QWidget | None = None):
'''
Construct a list view.
@param parent - parent widget
'''
super().__init__(parent)
self.setFrameShape(QFrame.Shape.NoFrame)
self.setBackgroundRole(QPalette.ColorRole.NoRole)
# end constructor
# end ListView
def tester():
''' Function to test the list view. '''
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import QApplication, QDialog, QHBoxLayout, QRadioButton, QVBoxLayout
def set_theme(dark: bool):
palette = QPalette()
if dark:
app.setStyle('Fusion')
palette.setColor(QPalette.ColorRole.Window, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.WindowText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Base, QColor(25, 25, 25))
palette.setColor(QPalette.ColorRole.AlternateBase, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.ToolTipBase, Qt.GlobalColor.black)
palette.setColor(QPalette.ColorRole.ToolTipText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Button, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.ButtonText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.BrightText, Qt.GlobalColor.red)
palette.setColor(QPalette.ColorRole.Link, QColor(42, 130, 218))
palette.setColor(QPalette.ColorRole.Highlight, QColor(42, 130, 218))
palette.setColor(QPalette.ColorRole.HighlightedText, Qt.GlobalColor.black)
#
else:
app.setStyle('windowsvista')
#
app.setPalette(palette)
# end set_theme
app = QApplication([])
win = QDialog()
win.setWindowTitle('List View example')
win.setLayout(QVBoxLayout())
lv = ListView()
win.layout().addWidget(lv)
row = QHBoxLayout()
win.layout().addLayout(row)
radio = QRadioButton('Light theme')
radio.setChecked(True)
radio.clicked.connect(lambda: set_theme(False))
row.addWidget(radio)
radio = QRadioButton('Dark theme')
radio.clicked.connect(lambda: set_theme(True))
row.addWidget(radio)
row.addStretch(1)
win.layout().addStretch(1)
win.show()
app.exec()
# end tester
if __name__ == '__main__': tester()