Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QListView item font StyleSheet not working.

    General and Desktop
    styesheet font qlistview item
    2
    5
    6932
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • IL
      IL last edited by

      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
      IL

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @IL last edited by raven-worx

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

        Edit:
        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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        IL 1 Reply Last reply Reply Quote 1
        • IL
          IL @raven-worx last edited by

          @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,
          IL

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @IL last edited by raven-worx

            @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 );
            }
            

            (untested)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            IL 1 Reply Last reply Reply Quote 1
            • IL
              IL @raven-worx last edited by

              @raven-worx
              Thanks bro,
              very helpfull.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post