How can I change the color of an item in a QListWidget
All Qt widgets have .setStyleSheet()
method which you can pass CSS to format widget, i.e…:
combo = QComboBox()
combo.setStyleSheet('QComboBox{{color: {black};background-color: {red};}} QComboBox QAbstractItemView {{background-color: {red};}}')
See mode info here Qt Style Sheets.
I was wrong: List of All Members for QListWidgetItem
There are .setBackground()
and .setForeground()
method.
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QColorConstants
from PySide6.QtWidgets import QApplication, QListWidget, QMainWindow, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QListWidget Example")
self.qlist = QListWidget()
self.qlist.addItems(['Item 1', 'Item 2', 'Item 3'])
for item in self.qlist.findItems('*', Qt.MatchFlag.MatchWildcard):
item.setBackground(QBrush(QColorConstants.Red))
layout = QVBoxLayout()
layout.addWidget(self.qlist)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
But probably you can control style of items of QListWidget
use .setStyleSheet()
directly on QListWidget
instance.
I’m not so familiar with, maybe this one: setStyleSheet
and here: Qt Style Sheets Reference
1 Like