I have two items in my tree model i have small difference in text alignment.Is this caused by the width of the text but i checked the width of text using QFontMetrics::width() but both text are same.
Text1:111601756
Text2:999999996
As from the image you can see there is a slight alignment problem in the second text.
Here is the sample code i tried :-
QFont font("times",24);
QFontMetrics metrics(font);
qDebug() << "Width 1" << metrics.width(QString::number(111111111));
qDebug() << "Width 2" << metrics.width(QString::number(999999999));
Output:
Width 1 153
Width 2 153
MyDelegate paint function:-
void LiDefaultTreeDelegate::paint(QPainter *painter, const
QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem newOption = option;
if(index.data(Qt::DisplayRole).toString() != NULL)
{
QString text = index.data(Qt::DisplayRole).toString();
QFontMetrics fnMetrics(fn);
newOption.rect = fnMetrics.boundingRect(text);
//Case 1
//newOption.rect.setWidth(fnMetrics.width(text));
//Case 2
//newOption.rect.setWidth(fnMetrics.width('0') * option.rect.width());
}
QStyledItemDelegate::paint(painter, newOption, index);
}
Now the problem is painting happens in the wrong area as from the image you can see the data gets painted on the top of root item. Any clue what I am missing here.
New Output: