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.
  • S Offline
    S Offline
    sitesv
    wrote on 30 Jul 2020, 17:04 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Jul 2020, 18:04 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

      S 1 Reply Last reply 30 Jul 2020, 19:43
      3
      • S SGaist
        30 Jul 2020, 18:04

        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.

        S Offline
        S Offline
        sitesv
        wrote on 30 Jul 2020, 19:43 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
        • E Offline
          E Offline
          eyllanesc
          wrote on 30 Jul 2020, 20:32 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.

          S 2 Replies Last reply 30 Jul 2020, 20:40
          1
          • E eyllanesc
            30 Jul 2020, 20:32

            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);
            
            S Offline
            S Offline
            sitesv
            wrote on 30 Jul 2020, 20:40 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?

            M 1 Reply Last reply 30 Jul 2020, 20:53
            0
            • S sitesv
              30 Jul 2020, 20:40

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 30 Jul 2020, 20:53 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
              • E eyllanesc
                30 Jul 2020, 20:32

                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);
                
                S Offline
                S Offline
                sitesv
                wrote on 30 Jul 2020, 21:07 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
                • E Offline
                  E Offline
                  eyllanesc
                  wrote on 30 Jul 2020, 21:27 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.

                  S 1 Reply Last reply 30 Jul 2020, 21:31
                  3
                  • E eyllanesc
                    30 Jul 2020, 21:27

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

                    S Offline
                    S Offline
                    sitesv
                    wrote on 30 Jul 2020, 21:31 last edited by
                    #9

                    @eyllanesc Thank you! It works!

                    1 Reply Last reply
                    0

                    1/9

                    30 Jul 2020, 17:04

                    • Login

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