Python Forum

Full Version: CSS Styling for a QLabel inside a QListWidget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Long story short, I use a QListWidget as a checklist. I would like to set a custom style for some items which are not checkable and rather act as text headers. I found this solution on Stack Overflow (last answer) for QTreeWidget, but I couldn't get it to work with QListWidget. Example;

#!/usr/bin/python3
from PyQt5 import QtWidgets


class Main(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__()
        self.listWidget = QtWidgets.QListWidget()
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.listWidget)
        self.setLayout(self.layout)
        self.show()

        item1 = QtWidgets.QListWidgetItem("Regular item")
        item2 = QtWidgets.QListWidgetItem("Label item")
        label = QtWidgets.QLabel()
        self.listWidget.addItem(item1)
        self.listWidget.addItem(item2)
        self.listWidget.setItemWidget(item2, label)

        style = """
        QListWidget::item {
            color: blue;
        }
        QLabel {
            color: red;
        }
        """
        parent.setStyleSheet(style)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main(app)
    app.exec()
[Image: Deepin-Screenshot-spacefm-20201128184259.png]

If that is not possible, does anyone know how to do the QStyledItemDelegate thing in PyQt?
The app use CSS quite heavily so I have to stick with that. Thanks.
The solution was to set the text in the QLabel widget instead of the QListWidgetItem, then to add 'QLabel after the ::item subcontrol of the CSS selector;

#!/usr/bin/python3
from PyQt5 import QtWidgets


class Main(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__()
        self.listWidget = QtWidgets.QListWidget()
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.listWidget)
        self.layout.addWidget(QtWidgets.QLabel("Regular Label"))
        self.setLayout(self.layout)
        self.show()

        item1 = QtWidgets.QListWidgetItem("List item")
        item2 = QtWidgets.QListWidgetItem()
        nestedLabel = QtWidgets.QLabel("Nested label")
        self.listWidget.addItem(item1)
        self.listWidget.addItem(item2)
        self.listWidget.setItemWidget(item2, nestedLabel)

        style = """
        QListWidget::item {
            color: blue;
        }
        QListWidget::item QLabel {
            color: red;
        }
        QLabel {
            color: green;
        }
        """
        parent.setStyleSheet(style)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main(app)
    app.exec()
[Image: Deepin-Screenshot-spacefm-20201128184259.png]
For anyone interested, you can either

1. Set an id selector;
obj.setObjectName("example")
then in CSS;
Quote:QLabel#example { color: red; }

2. Assign a CSS class to a QObject;
obj.setProperty("class", "example")
then in CSS;
Quote:.example { color: red; }