QFontMetrics::boundingRect() and QPainter::draw() differences.
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.)
Code:
#include <QtGui>
#include <QtWidgets>
{
Q_OBJECT
public:
explicit Delegate
(QObject *parent
= 0) , m_padding(6)
{}
{
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;
}
{
bool selected
= option.
state & QStyle::State_Selected;
if (selected)
painter->fillRect(option.rect, option.palette.highlight());
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[])
{
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.";
Delegate delegate;
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
Re: QFontMetrics::boundingRect() and QPainter::draw() differences.
I had similar problems a couple of days ago. For example I noticed there is a difference in result between this call:
and this one:
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.
Re: QFontMetrics::boundingRect() and QPainter::draw() differences.
Quote:
Originally Posted by
wysota
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.
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.
Re: QFontMetrics::boundingRect() and QPainter::draw() differences.
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.
Code:
QRectF rectFromText
( const QModelIndex
& index,
QPainter* painter
= nullptr
) const {
if( painter != nullptr )
{
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
);
}
// 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.