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. How to change the height and text font size of qtreeview item
Forum Updated to NodeBB v4.3 + New Features

How to change the height and text font size of qtreeview item

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NoumanYosuf
    wrote on last edited by
    #1

    Hi all,
    I have a treeview and I want to change the height of each item (QModelIndex) and also change the font size to fit into it well.
    I found a way to change the size of each treeview item (50,as i needed), but unable to figure out how to change the font size.
    Below is the code :

    class Delegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        QSize sizeHint(const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const //this function to compensate the alignment
        {
            QSize size = QStyledItemDelegate::sizeHint(p_option, p_index);
            if (p_index.column() == 1 || p_index.column() == 0)
            {
                size.setHeight(100);
            }
            return size;
        }
    };
    
    tree->setItemDelegate(new Delegate());
    

    Capture.PNG
    Also I know I can change the modelIndex heigth from model also, but again I am not sure how to set the font size.
    Could anyone please let me a way to achieve this.

    Gojir4G 1 Reply Last reply
    0
    • N NoumanYosuf

      Hi all,
      I have a treeview and I want to change the height of each item (QModelIndex) and also change the font size to fit into it well.
      I found a way to change the size of each treeview item (50,as i needed), but unable to figure out how to change the font size.
      Below is the code :

      class Delegate : public QStyledItemDelegate
      {
          Q_OBJECT
      public:
          QSize sizeHint(const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const //this function to compensate the alignment
          {
              QSize size = QStyledItemDelegate::sizeHint(p_option, p_index);
              if (p_index.column() == 1 || p_index.column() == 0)
              {
                  size.setHeight(100);
              }
              return size;
          }
      };
      
      tree->setItemDelegate(new Delegate());
      

      Capture.PNG
      Also I know I can change the modelIndex heigth from model also, but again I am not sure how to set the font size.
      Could anyone please let me a way to achieve this.

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hi,
      I guess you could reimplement QStyledItemDelegate::initStyleOption()

      void Delegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
          QStyledItemDelegate::initStyleOptions(option, index);
          if (p_index.column() == 1 || p_index.column() == 0)
          {
              option->font.setPointSize(20);
              //or setPixelSize(...);
              //or setPointSizeF(...);
          }
      }
      

      font is a public variable from QStyleOptionViewItem

      1 Reply Last reply
      3
      • N Offline
        N Offline
        NoumanYosuf
        wrote on last edited by NoumanYosuf
        #3

        @Gojir4 Thank you for you reply.
        It tired this but the content of the treeitem is missing now.
        I tried all the options :

        option->font.setPointSize(50);
        option->font.setPixelSize(50);
        option->font.setPointSizeF(50);
        

        .Capture.PNG

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you also call the base implementation of initStyleOption()? I would guess no.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          Gojir4G 1 Reply Last reply
          4
          • Christian EhrlicherC Christian Ehrlicher

            Do you also call the base implementation of initStyleOption()? I would guess no.

            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by Gojir4
            #5

            @NoumanYosuf @Christian-Ehrlicher My bad. I did correct the code it should work better now.

            1 Reply Last reply
            1
            • N Offline
              N Offline
              NoumanYosuf
              wrote on last edited by
              #6

              Hey @Christian-Ehrlicher and @Gojir4 yes its works fine. thank you both for the help.

              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