Styling the QListItemWidget's Widget



  • I have a QListWidget, and in it are various QListWidgetItem(s). On each QListWidgetItem there is a widget set by setItemWidget().

    I can style the whole QListWidgetItem

    QListWidget::item:selected {
        background: #efefef;
    }
    

    But if I want to change the 'color' of the QLabel inside QWidget which is inside QListWidgetItem (selected by ::item) like this

    QListWidget::item:selected {
        background: #efefef;
        color: white;
    }
    

    It does not work.

    Using CSS I want to change the color of the Item's Widget, on 'selected' state.

    More specifcally I want to change the color of the QLabel(s) inside the QWidget which is inside QListWidgetItem.

    Is this possible? How can I achieve this?


  • Qt Champions 2017

    Hi
    you have to target QLabel then, not just QListWidget::item

    QLabel {
    xxxxx
    }

    If it need to be just one, you can use names
    QLabel#somename {
    xxxxx
    }



  • @mrjj
    I don't think I understand properly.
    I have done this before

    QListWidget::item:selected QLabel {
        background:#efefef;
        color:white;
    }
    

    It does styles the QLabels but not on 'selected' state. This applies to the QLabels even when QListWidgetItem is not selected. I want to apply the styles on selected state.
    Can you post the complete block.. so I can understand better?


  • Qt Champions 2017

    @candh
    I dont think substates affects the children.
    so QListWidget::item:selected wont affect QLabel.
    Did Not work in my test.

    Maybe you could use dynamic properties.
    https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
    so when you click item, you grab the widget and
    set the dynamic property.



  • @mrjj
    So, If I understand correctly, I have to set up a on click listener on the QWidget or QLabel so I can call the setProperty() and polish/unpolish methods on them and the CSS will update the widget.
    Am I correct?


  • Qt Champions 2017

    @candh
    Yes, or on the QListWidget signal for something selected if that fires when you
    click on the widgets.
    I used the custom/dynamic property to allow stylesheet to define the selection colors. I assume your goal is somewhat the same.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.