View Full Version : How to reimplement sizeHint()?

6th July 2010, 13:07
I'm making a subclass of QGraphicsLayoutItem, but can't quite figure out, how to correctly reimplement sizeHint() function. Here's what I got now:

QSizeF PLFTextItem::sizeHint (Qt::SizeHint _which, const QSizeF &_constraint) const
switch (_which)
case Qt::MinimumSize: return m_sizeHint;
case Qt::PreferredSize: return _constraint.expandedTo (m_sizeHint);
return QSizeF();

Where m_sizeHint bears the actual size of an object being displayed (in this case, simple text item). Also, the size policy for this item is (QSizePolicy::Preferred, QSizePolicy::Fixed), and stretch, both horizontal and vertical, is 0.

The problem is, when I put this layout item into, say, a linear layout with a couple of others, horizontally it takes more space than it needs, and also takes it somewhat proportionally to m_sizeHint. What am I doing wrong?

Here is a screenshot of my QGraphicsScene, to make things clear:

And while we are at it, I wonder if I'm doing the whole thing entirely wrong: subclassing QGraphicsLayoutItem just to handle QGraphicsSimpleTextItem with layouts. It sure seems a little awkward, but QGraphicsLayoutItem::setGraphicsItem() is protected, and you still need to move QGraphicsItem in question when setGeometry is called. Is there any simpler way? But even if there is, I'll be doing some more complex stuff inside another QGraphicsLayoutItem, and I'd probably want to sublcass anyway. So the question still stands.