PDA

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



Lykurg
27th February 2013, 10: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, 11: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, 12: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.

Blanky
9th June 2022, 16:29
I know this a super old post, but I came across the same issue and solved the issue by doing the following helper function in my subclassed QStyledItemDelegate class.



QRectF rectFromText( const QModelIndex& index, QPainter* painter = nullptr ) const
{
QString text = index.data( Qt::UserRole + 1 ).value< QString >();
QRectF font_rect;

if( painter != nullptr )
{
QTextOption text_options;
text_options.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
text_options.setAlignment( Qt::AlignLeft | Qt::AlignVCenter );

font_rect = painter->boundingRect( QRectF( 0, 0, parent_list_widget_->width() * 0.5, 10000 ), text, text_options );
const_cast< QAbstractItemModel* >( index.model() )->setData( index, QVariant::fromValue( font_rect ), Qt::UserRole + 2 );
}
// If we don't have a QPainter, this probably means we're calling from contexts for
// tooltip positioning, sizeHint, and or the click events from the parent list widget.
// We're pretty much guaranteed that the paint event would've drawn it by now, before querying this,
// it should always be populated by here.
else
{
font_rect = index.data( Qt::UserRole + 2 ).value< QRectF >();
}

return font_rect;
}


This gave the ability for the painter and any other function to retrieve what the painted size would be and it worked for QAbstractItemDelegate::helpEvent, QAbstractItemDelegate::paint, QAbstractItemDelegate::sizeHint, and even outside contexts of the delegate, like click events from the perspective of a parental QListWidget. I used this to make a chat widget, so sender and receiver would have their bounding rectangles positions relative to left or right aligned sides depending on who was the sender. I only wanted click events to be within the area for that item rather than the entire row, like it normally would. Hopefully this helps someone, because I wish I had this knowledge before delving down the rabbit hole.