QListView item font StyleSheet not working.

  • Hi All,
    When I try to change QListView item StyleSheet there is no effect for all font styling issue while background styling working just fine. When I set stylesheet to the whole QListView I can see the change properly
    For example:

    QListView{background-color: black; color: white ;font-size: 16px; outline:none;font-weight:bold} // Working.
    QListView::item:selected{font:bold}  // Not working!
    QListView::item:selected{background-color: rgb(14, 97,123)} // Working.

    What an I doing wrong? please advice

  • Moderators

    since you only setting the font's weight try font-weight instead

    seems like setting the font via stylesheet for the ::itemsubcotnrol isn't supported at all. Only setting the font on the item-widget itself, but then the font gets applied for all items.

    You may try to return a font in your model's data() method for the Qt::FontRole role. This should work.

  • @raven-worx
    Thanks for reply,
    It is pity to hear that font stylesheet for ::item subcotnrol isn't supported.
    Basicaly I need to change the selected font item into bold, font-weight also does'nt work.
    Can you send me link to example with Qt::FontRole about this issue?

    Best regards,

  • Moderators

    @IL said:

    Can you send me link to example with Qt::FontRole about this issue?

    QVariant MyModel::data(const QModelIndex &index, int role) const
          QVariant v = ModelBaseClass::data(index,role);
          if( condition && role == Qt::FontRole )
                   QFont font = v.value<QFont>();
                        font.setBold( true );
                   v = QVariant::fromValue<QFont>( font );
          return v;

    Or even easier using the QStyledItemDelegate-way (since you want it for selected items?):

    void MyItemDelegate::initStyleOption(QStyleOptionViewItem * option, const QModelIndex & index) const
         QStyledItemDelegate::initStyleOption(option, index);
         if( option->state & QStyle::State_Selected )
              option->font.setBold( true );


  • @raven-worx
    Thanks bro,
    very helpfull.