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. Different font size of two strings in QTeeView
Qt 6.11 is out! See what's new in the release blog

Different font size of two strings in QTeeView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I have an application in which i am using custom delegate to display two rows to display text.
    Now i want to change font of both of these strings, But i don't have any idea how to do so in custom delegate.
    My custom delegate class is as following

    @
    TreeViewItemDelegate::TreeViewItemDelegate(QObject *parent)
    : QItemDelegate(parent)
    {
    }

    void TreeViewItemDelegate ::paint( QPainter * painter,
    const QStyleOptionViewItem & option,
    const QModelIndex & index ) const
    {
    QString contactURI = index.model()->data(index, Qt::DisplayRole).toString();
    QIcon icon = qvariant_cast<QIcon>(index.model()->data(index, Qt::DecorationRole));
    QIcon icon2 = qvariant_cast<QIcon>(index.model()->data(index, Qt::UserRole+5));
    QIcon icon3 = qvariant_cast<QIcon>(index.model()->data(index, Qt::UserRole+6));
    QString freeText = index.model()->data(index, Qt::UserRole+2).toString();

    QPixmap iconPixmap = icon.pixmap(option.decorationSize);
    QPixmap iconPixmap2 = icon2.pixmap((option.decorationSize/3)*2);
    QPixmap iconPixmap3 = icon3.pixmap((option.decorationSize/3)*2);
    
    QStyleOptionViewItem myoption =  option;
    
    QRect rect1 = this->calcDecorationRect(option.rect, Qt::AlignLeft, Qt::AlignTop);
    QRect rect2 = this->calcDecorationRect(option.rect, Qt::AlignRight, Qt::AlignTop);
    

    // QRect rect3 = this->calcDecorationRect(option.rect, Qt::AlignLeft, Qt::AlignBottom);
    QRect rect4 = this->calcDecorationRect(option.rect, Qt::AlignRight, Qt::AlignBottom);

    QRect displayRect = this->calcDisplayRect(option.rect, Qt::AlignTop);
    QRect freeTextRect = this->calcDisplayRect(option.rect, Qt::AlignBottom);
    
    drawBackground(painter, myoption, index);
    drawDecoration(painter, myoption, rect1, iconPixmap);
    drawDisplay(painter,myoption, displayRect, contactURI);
    drawDisplay(painter,myoption, freeTextRect, freeText);
    drawDecoration(painter, myoption, rect2, iconPixmap2);
    //drawDecoration(painter, myoption, rect3, iconPixmap2);
    drawDecoration(painter, myoption, rect4, iconPixmap3);
    drawFocus(painter, myoption,option.rect);
    

    }
    QRect TreeViewItemDelegate :: calcDecorationRect(QRect mainRect, int horizontalAlignment, int verticalAlignment) const
    {
    QRect rect;

    if(horizontalAlignment == Qt::AlignLeft)
    {
        if(verticalAlignment == Qt::AlignTop)
        {
            rect.setX(mainRect.x()+2);
            rect.setY(mainRect.y()+2);
            rect.setWidth(mainRect.width()/8);
            rect.setHeight((mainRect.height())-6);
        }
    

    // else if(verticalAlignment == Qt::AlignBottom)
    // {
    // rect.setX(mainRect.x()+ 2);
    // rect.setY(mainRect.y()+ (mainRect.height()/3)*2 - 2);
    // rect.setWidth(mainRect.width()/8);
    // rect.setHeight(mainRect.height()/3);
    // }
    }
    if(horizontalAlignment == Qt::AlignRight)
    {
    if(verticalAlignment == Qt::AlignTop)
    {
    rect.setX(mainRect.x() + (mainRect.width()/8)*7 - 2);
    rect.setY(mainRect.y() + 4);
    rect.setWidth(mainRect.width()/8);
    rect.setHeight(mainRect.height()/3);
    }
    else if(verticalAlignment == Qt::AlignBottom)
    {
    rect.setX(mainRect.x() + (mainRect.width()/8)*7 - 2);
    rect.setY(mainRect.y()+ (mainRect.height()/3)*2 - 4);
    rect.setWidth(mainRect.width()/8);
    rect.setHeight(mainRect.height()/3);
    }
    }
    return rect;
    }
    QRect TreeViewItemDelegate :: calcDisplayRect(QRect mainRect, int verticalAlignment) const
    {

    QRect rect;
    if(verticalAlignment == Qt::AlignTop)
    {
        rect.setX(mainRect.x() + mainRect.width()/8 + 3 );
        rect.setY(mainRect.y() + 2);
        rect.setHeight(mainRect.height()/2 - 2);
        rect.setWidth(mainRect.width() - 2*(mainRect.width()/8));
    }
    else if(verticalAlignment == Qt::AlignBottom)
    {
        rect.setX(mainRect.x() + mainRect.width()/8 + 3 );
        rect.setY(mainRect.y() + mainRect.height()/2);
        rect.setHeight(mainRect.height()/2 - 2);
        rect.setWidth(mainRect.width() - 2*(mainRect.width()/8));
    }
    
    return rect;
    

    }
    @
    Can any one suggest me for that?

    Thanks in advance.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Did you notice how the arguments for drawDisplay contain an options parameter? That options parameter contains a font...

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Yes i know that, but when i am trying to set font with option.font. By doing so it set same font for both string whereas i want to change font of both string (contactURI , freeText).
        Then how can i set the different font for them??

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          You are using a different option for both calls to drawDisplay, right?

          Anyway, you can also draw the text yourself using QPainter. That gives you full control over the appearance.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Oh ya, thanks.

            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