[Interest] [QGView] Center align text item that ignores inherited transformation
Ch'Gans
chgans at gna.org
Thu Nov 24 01:28:19 CET 2016
On 24 November 2016 at 01:43, Majid Kamali <majidkamali1370 at gmail.com> wrote:
> Hi. Check this code:
>
> void centerItemInRect(QGraphicsItem *item, QRectF rect)
> {
> float x = rect.width() -
> item->boundingRegion(item->transform()).boundingRect().width();
> x /= 2;
> float y = rect.height() -
> item->boundingRegion(item->transform()).boundingRect().height();
> y /= 2;
>
> item->setPos(x + rect.x(), y + rect.y());
> }
>
> This code works if you use Transformation on QGItem. It does not work if you
> set scale and rotation with setRotation and setScale.
Thanks for sharing, but this does not work when the item has the
ItemIgnoresTransformation flag set.
I think it's simply not possible to center-align a QGSimpleTextItem
without writing a custom QGItem wrapper of re-writing a simple text
item from scratch...
The main problem is that the bounding region/rect of the item is
expressed in device coordinates (pixels), and the item transform is
relative to parent (or scene) coordinates, so you cannot mix both.
Chris
>
>
> On Wed, Nov 23, 2016 at 4:15 AM, Ch'Gans <chgans at gna.org> wrote:
>>
>> Hi there,
>>
>> I'm trying to add a QGraphicsSimpleTextItem as a child of another (shape)
>> item.
>> I want the text to not follow scaling of the view (I use
>> QGraphicsItem::ItemIgnoreTransformations flag), *and* be always
>> centered on it's parent shape.
>> Ideally, i'm looking at a solution that doesn't require to create a
>> custom QGItem.
>>
>> My problem is the centering of the text item itself, without
>> centering, here is what i do:
>>
>> ------------------------------------------------------------------------------------------
>> auto item = new ...
>> ...
>> auto text = new QGraphicsSimpleTextItem(item);
>> text->setText(...);
>> text->setFlag(QGraphicsItem::ItemIgnoresTransformations);
>> auto itemCenter = QPointF(item->boundingRect().width()/2.0,
>>
>> item->boundingRect().height()/2.0);
>> text->setPos(itemCenter);
>>
>> ------------------------------------------------------------------------------------------
>>
>> This work fine, but (as expected) the top-left of the text is
>> positioned on the center of the parent item.
>> To center-align, I tried:
>>
>> -------------------------------------------------------------------------------------------
>> auto textCenter = QPointF(text->boundingRect().width()/2.0,
>>
>> text->boundingRect().height()/2.0);
>> text->setTransform(QTransform::fromTranslate(-textCenter.x, -textCenter));
>>
>> -------------------------------------------------------------------------------------------
>>
>> But this doesn't work because textCenter is an offset in a coordinate
>> system that doesn't follow view scaling... and the text transform
>> seems to be applied in the "scaled space".
>>
>> Basically my problem is that it seems that I need a dynamic
>> positioning wrt the parent that depends on the view transform...
>>
>> Does anyone know some tricks on how to achieve this?
>>
>> Thanks,
>> Chris
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>
>
More information about the Interest
mailing list