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.
Forum Updated to NodeBB v4.3 + New Features

QListView item font StyleSheet not working.

Scheduled Pinned Locked Moved Solved General and Desktop
styesheetfontqlistviewitem
5 Posts 2 Posters 8.3k 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.
  • I Offline
    I Offline
    IL
    wrote on 8 Aug 2016, 10:42 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

    R 1 Reply Last reply 8 Aug 2016, 11:24
    0
    • I IL
      8 Aug 2016, 10:42

      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

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 8 Aug 2016, 11:24 last edited by raven-worx 8 Aug 2016, 11:33
      #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

      I 1 Reply Last reply 8 Aug 2016, 12:31
      1
      • R raven-worx
        8 Aug 2016, 11:24

        @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.

        I Offline
        I Offline
        IL
        wrote on 8 Aug 2016, 12:31 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

        R 1 Reply Last reply 8 Aug 2016, 13:27
        0
        • I IL
          8 Aug 2016, 12:31

          @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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 8 Aug 2016, 13:27 last edited by raven-worx 8 Aug 2016, 13:32
          #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

          I 1 Reply Last reply 16 Aug 2016, 15:11
          1
          • R raven-worx
            8 Aug 2016, 13:27

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

            I Offline
            I Offline
            IL
            wrote on 16 Aug 2016, 15:11 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