PDA

View Full Version : sizeHint based off visible text



joshuajcarson
24th September 2008, 17:06
I've searched the forums for sizeHint, but I have been unable to find out to figure out a good sizeHint based off the visible text when displaying html. Currently the sizeHint works wonderfully for all the text in the cell (I just see how many characters are in the QTextDocument), but not so good for just giving a good sizeHint for the characters my user will actually see. Can someone point me in the right direction?

joshuajcarson
25th September 2008, 16:51
I've found a solution that works. Here is the code


if (qVariantCanConvert<QString>( index.data() ) ) {
QString htmlText = qVariantValue<QString>(index.data());
QTextDocument qtd;
qtd.setHtml(htmlText);
qtd.setTextWidth(-1);
QSize toRet;
toRet.setWidth( ceil( qtd.idealWidth() ) );
toRet.setHeight( ceil( qtd.size().height() ) );
return toRet;
} else {
return QItemDelegate::sizeHint(option, index);
}

I'm sure there might be a better way, but this seems to work. The QTextDocument gives back a QSizeF, which means it might round down when it should be rounding up all the time. If someone knows of a better way to do this, I'm all ears.