Different font size of two strings in QTeeView



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



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



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



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



  • Oh ya, thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.