PDA

View Full Version : Using multiple QFonts in QTreeView



Geropy
24th October 2011, 16:30
Hello,

I am trying to create a QTreeView that can have different fonts used for different rows of the tree. I know that I can do this with the QTreeWidget convenience class, but I need some extra functionality that this class can't provide (e.g. making only a certain column editable).

I think that the way to do this is by subclassing QTreeView and using different QPainters for different rows. However, I am unable to get my TreeView class to use my painter's font.

This is currently what I have in my subclassed TreeView's paintEvent function (it does not use the specified font):


void TreeView::paintEvent(QPaintEvent *event)
{
QPainter painter(this->viewport());
QFont serifFont("Times", 11, QFont::Bold);
painter.setFont(serifFont);
drawTree(&painter, event->region());

}

However, if I code it this way instead, it does end up using "serifFont".


void TreeView::paintEvent(QPaintEvent *event)
{
QPainter painter(this->viewport());
QFont serifFont("Times", 11, QFont::Bold);
setFont(serifFont);
drawTree(&painter, event->region());

}

Can anybody tell me how to get the TreeView to use the painter's font? Or is there a better way of having multiple fonts in a QTreeView without using the convenience classes?

Thanks

Santosh Reddy
2nd November 2011, 01:18
QTreeView can uses the font provided by model, you can return the required font as per row & column from the model instead setting in custom view. Check for Qt::FontRole in data () in model implementation