Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView item font StyleSheet not working.
QtWS25 Last Chance

QListView item font StyleSheet not working.

Scheduled Pinned Locked Moved Solved General and Desktop
styesheetfontqlistviewitem
5 Posts 2 Posters 8.2k Views
  • 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.
  • ILI Offline
    ILI Offline
    IL
    wrote on last edited by
    #1

    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-worxR 1 Reply Last reply
    0
    • ILI IL

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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

      ILI 1 Reply Last reply
      1
      • raven-worxR 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.

        ILI Offline
        ILI Offline
        IL
        wrote on last edited by
        #3

        @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-worxR 1 Reply Last reply
        0
        • ILI IL

          @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-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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

          ILI 1 Reply Last reply
          1
          • raven-worxR 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)

            ILI Offline
            ILI Offline
            IL
            wrote on last edited by
            #5

            @raven-worx
            Thanks bro,
            very helpfull.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved