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. QTreeWidget: change font size
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget: change font size

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.3k Views 3 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.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by
    #1

    Hello!
    Is it possible to change the font size while using QStyledItemDelegate?
    In 'paint" section trying to do next:

    QStyleOptionViewItemV4 viewOption = option;
    initStyleOption(&viewOption, index);
    viewOption.font.setPixelSize(8);
    

    But it is still not working...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please show the complete code of your custom item delegated.

      Note that if it's just a question of font, you can set the Qt::FontRole of you model items.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      sitesvS 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        Please show the complete code of your custom item delegated.

        Note that if it's just a question of font, you can set the Qt::FontRole of you model items.

        sitesvS Offline
        sitesvS Offline
        sitesv
        wrote on last edited by sitesv
        #3

        @SGaist

        void TfDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            QStyleOptionViewItemV4 viewOption = option;
            initStyleOption(&viewOption, index);
            viewOption.font.setPixelSize(8);
        
            painter->save();
        
            QBrush brush = QBrush(Qt::red);
            QRect rect = viewOption.rect;
        
            //make some drawing
            painter->fillRect(rect, brush);
        
            //draw text and frame
            QPen pen;
            pen.setColor(Qt::black);
            pen.setWidth(2);
            painter->setPen(pen);
            painter->drawText(rect, Qt::AlignCenter, "Cell");
            painter->drawRect(rect); 
        }
        

        @SGaist said in QTreeWidget: change font size:

        Note that if it's just a question of font, you can set the Qt::FontRole of you model items.

        Could you please explain this?
        Something like this?:

        QFont fff;
        fff.setPixelSize(8);
        index.model()->data(index, Qt::FontRole).setValue(fff);
        
        1 Reply Last reply
        0
        • eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          Two options:

          1. override initStyleOption and don't modify the paint method
          void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){
              QStyledItemDelegate::initStyleOption(option, index);
              option->font.setPixelSize(8);
          }
          

          Or

          1. Use the model
          QFont fff;
          fff.setPixelSize(8);
          index.model()->setData(index, fff,  Qt::FontRole);
          

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          sitesvS 2 Replies Last reply
          1
          • eyllanescE eyllanesc

            Two options:

            1. override initStyleOption and don't modify the paint method
            void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){
                QStyledItemDelegate::initStyleOption(option, index);
                option->font.setPixelSize(8);
            }
            

            Or

            1. Use the model
            QFont fff;
            fff.setPixelSize(8);
            index.model()->setData(index, fff,  Qt::FontRole);
            
            sitesvS Offline
            sitesvS Offline
            sitesv
            wrote on last edited by
            #5

            @eyllanesc said in QTreeWidget: change font size:

            index.model()->setData(index, fff, Qt::FontRole);

            Hi!
            Trying to do this in 'paint' event of my delegate.
            There is an error:

            form_board_delegate.cpp:40:5: error: 'this' argument to member function 'setData' has type 'const QAbstractItemModel', but function is not marked const
            qabstractitemmodel.h:192:30: note: 'setData' declared here
            

            I'm supposed that this is valid only for QTreView, not for QTreeWidget. Right?

            mrjjM 1 Reply Last reply
            0
            • sitesvS sitesv

              @eyllanesc said in QTreeWidget: change font size:

              index.model()->setData(index, fff, Qt::FontRole);

              Hi!
              Trying to do this in 'paint' event of my delegate.
              There is an error:

              form_board_delegate.cpp:40:5: error: 'this' argument to member function 'setData' has type 'const QAbstractItemModel', but function is not marked const
              qabstractitemmodel.h:192:30: note: 'setData' declared here
              

              I'm supposed that this is valid only for QTreView, not for QTreeWidget. Right?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sitesv
              Hi
              It also works with *Widget version.
              Just set on the item and not model.

                    auto item = new QTreeWidgetItem(ui->treeWidget);
                    QFont font;
                    font.setPixelSize(8);
                    item->setData(0,Qt::FontRole,font);
                    item->setText(0,"MUHA");
                    ui->treeWidget->addTopLevelItem(item);
              

              alt text

              1 Reply Last reply
              1
              • eyllanescE eyllanesc

                Two options:

                1. override initStyleOption and don't modify the paint method
                void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){
                    QStyledItemDelegate::initStyleOption(option, index);
                    option->font.setPixelSize(8);
                }
                

                Or

                1. Use the model
                QFont fff;
                fff.setPixelSize(8);
                index.model()->setData(index, fff,  Qt::FontRole);
                
                sitesvS Offline
                sitesvS Offline
                sitesv
                wrote on last edited by sitesv
                #7

                @eyllanesc said in QTreeWidget: change font size:

                > Two options:
                > 
                > 1. override initStyleOption and don't modify the paint method
                > 
                > void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){
                >     QStyledItemDelegate::initStyleOption(option, index);
                >     option->font.setPixelSize(8);
                > }
                

                Not working for me,

                @mrjj said in QTreeWidget: change font size:

                auto item = new QTreeWidgetItem(ui->treeWidget);
                QFont font;
                font.setPixelSize(8);
                item->setData(0,Qt::FontRole,font);
                item->setText(0,"MUHA");
                ui->treeWidget->addTopLevelItem(item);

                Sorry. My case is QTableWidget, not a QTreeWidget.

                 newItem = new QTableWidgetItem("");
                 newItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
                
                 QFont font;
                 font.setPixelSize(8);
                 newItem->setText("TEXT");
                 newItem->setData(Qt::FontRole, font);
                

                This is also not working in my case.

                Maybe something with 'paint' method?

                I have only one font size for all items... :(

                1 Reply Last reply
                0
                • eyllanescE Offline
                  eyllanescE Offline
                  eyllanesc
                  wrote on last edited by eyllanesc
                  #8

                  @sitesv

                  1. You cannot and should not execute the setData method inside the paint.

                  2. have you modified the delegate's paint method? If so then you must set the font directly to the QPainter: painter->setFont(fff).

                  My previous options assume that you are not doing a custom painting (by default the painting is done by QStyle using the attributes of "option").

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  sitesvS 1 Reply Last reply
                  3
                  • eyllanescE eyllanesc

                    @sitesv

                    1. You cannot and should not execute the setData method inside the paint.

                    2. have you modified the delegate's paint method? If so then you must set the font directly to the QPainter: painter->setFont(fff).

                    My previous options assume that you are not doing a custom painting (by default the painting is done by QStyle using the attributes of "option").

                    sitesvS Offline
                    sitesvS Offline
                    sitesv
                    wrote on last edited by
                    #9

                    @eyllanesc Thank you! It works!

                    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