Python Forum
[PyQt] CSS Styling for a QLabel inside a QListWidget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] CSS Styling for a QLabel inside a QListWidget
#1
Question 
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.
Reply
#2
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]
Reply
#3
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; }
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get the color name of qlistwidget? flash77 5 1,193 Aug-13-2023, 07:01 PM
Last Post: flash77
  QListWidget, no item was selected flash77 4 924 Aug-02-2023, 09:31 AM
Last Post: Axel_Erfurt
  GUI Problem / call another function / fill QListwidget flash77 5 836 Jul-30-2023, 04:29 PM
Last Post: flash77
  [PyQt] populate a QListWidget devilonline 1 1,524 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  Scaling text QLabel following display mode windows '100%, 125% ...) VIGNEAUD 2 2,217 Jul-07-2021, 06:38 PM
Last Post: deanhystad
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,747 May-06-2020, 06:11 PM
Last Post: karolp
  Why QListWidget doesn't show new line? AlekseyPython 3 3,315 Feb-05-2019, 02:23 PM
Last Post: AlekseyPython
  [PyQt] Number of characters in QLabel hzcodec 2 4,821 Oct-13-2017, 10:11 AM
Last Post: hzcodec

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020