PDA

View Full Version : QFontMetrics::boundingRect() and QPainter::draw() differences.



Lykurg
27th February 2013, 11:02
Hi,

I made a delegate for multiline entries for QListView. The problem I am facing right now is that the sizehint method is not accurate. If you run the sample code and slowly change the width of the listview you will recognice that on specific width a "item line" is too high. (sizeHint is calculating one text line too much. QPainter on the other hand can render the text in X - 1 line.)


#include <QtGui>
#include <QtWidgets>

class Delegate : public QItemDelegate
{
Q_OBJECT
public:
explicit Delegate(QObject *parent = 0)
: QItemDelegate(parent)
, m_padding(6)
{}

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const QFontMetrics fm = option.fontMetrics;
const QString &text = index.data().toString();
const QRect r = QRect(0, 0, option.rect.width() - 2 * m_padding, 0);

QSize s = option.fontMetrics.boundingRect(r, Qt::AlignLeft | Qt::TextWordWrap, text).size();
s.setHeight(s.height() + 2 * m_padding);
s.setWidth(s.width() + 2 * m_padding);
return s;
}

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool selected = option.state & QStyle::State_Selected;

if (selected)
painter->fillRect(option.rect, option.palette.highlight());
else if (option.features & QStyleOptionViewItem::Alternate)
painter->fillRect(option.rect, option.palette.alternateBase());

painter->setPen(selected
? option.palette.highlightedText().color()
: option.palette.text().color());

const QRect r = option.rect.adjusted(m_padding , m_padding , -m_padding , -m_padding);
painter->drawText(r, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString());
}

private:
int m_padding;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStringList l;
l << "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel ligula tellus.";
l << "Quisque lorem leo, porta in bibendum vitae, pellentesque mollis dui. Suspendisse viverra sollicitudin volutpat. Pellentesque arcu est, dignissim vel tempus at, facilisis et diam.";
l << "Suspendisse est urna, feugiat vel dignissim ut, aliquet nec eros.";

QStringListModel model(l);
Delegate delegate;

QListView view;
view.setModel(&model);
view.setItemDelegate(&delegate);
view.setWordWrap(true);
view.setAlternatingRowColors(true);
view.show();

return a.exec();
}

#include "main.moc"


It is because QFontMetrics::boundingRect() is returning a larger bounding box, because it uses also the maximum left and right font bearings. But how can I calculate them in sizeHint? I have tried different calculation but all failed.


Thanks,
Lykurg

wysota
27th February 2013, 12:00
I had similar problems a couple of days ago. For example I noticed there is a difference in result between this call:

QSize s = QFontMetrics(...).boundingRect("Some text").size();
and this one:

QSize s = QFontMetrics(...).boundingRect(QRect(100, 100, 100, 100), 0, "Some text").size();

Maybe try passing the real rect the text is to occupy, e.g. option.rect.adjusted(m_padding, m_padding, -m_padding, -m_padding). Maybe you'll get better results.

By the way, isn't the default delegate already wrapping lines in some cases?

Another solution would be to go through QTextLayout or QStaticText.

Lykurg
27th February 2013, 13:24
Maybe try passing the real rect the text is to occupy, e.g. option.rect.adjusted(m_padding, m_padding, -m_padding, -m_padding). Maybe you'll get better results.

By the way, isn't the default delegate already wrapping lines in some cases?
Well that was my first attempt to call QItemDelegate::sizeHint and then deal with my padding. Since that failed, I continued to calculate myself... I had allready also tried to pass the real rect. Same missbehavior.


Another solution would be to go through QTextLayout or QStaticText. After a quick shot with QTextLayout things get worser :eek: But that is probably because I am not familiar with QTextLayout. I'll dig deeper and see if I can manage it.

Thanks for the tip.