QGraphicstextitem dynamic resize and changing top-left position of item
-
For resizing QGraphicsRectItem and QGraphicsEllipseItem, setRect() function is available to resize as well as change location of the item (top-left corner of item).
class BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect) = 0;
};class RectResizer : public BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QGraphicsRectItem* rectItem = dynamic_cast<QGraphicsRectItem*>(item);if (rectItem) { rectItem->setRect(rect); } }};
In QGraphicsTextItem, only setTextWidth() is available, which can only modify width. If i perform resize from top-left corner, along with width change, top-left position also changes. Then how do i move QGraphicsTextItem to new position ?
class TextResizer : public BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QGraphicsTextItem* textItem = dynamic_cast<QGraphicsTextItem*>(item);if (textItem) { textItem->setTextWidth(rect.width()); } }};
-
For resizing QGraphicsRectItem and QGraphicsEllipseItem, setRect() function is available to resize as well as change location of the item (top-left corner of item).
class BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect) = 0;
};class RectResizer : public BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QGraphicsRectItem* rectItem = dynamic_cast<QGraphicsRectItem*>(item);if (rectItem) { rectItem->setRect(rect); } }};
In QGraphicsTextItem, only setTextWidth() is available, which can only modify width. If i perform resize from top-left corner, along with width change, top-left position also changes. Then how do i move QGraphicsTextItem to new position ?
class TextResizer : public BoundingBoxSetter
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QGraphicsTextItem* textItem = dynamic_cast<QGraphicsTextItem*>(item);if (textItem) { textItem->setTextWidth(rect.width()); } }};
@Sridharan said in QGraphicstextitem dynamic resize and changing top-left position of item:
Then how do i move QGraphicsTextItem to new position ?
I don't really understand what you are asking, but every graphics item inherits void QGraphicsItem::setPos(const QPointF &pos).
-
@Sridharan said in QGraphicstextitem dynamic resize and changing top-left position of item:
Then how do i move QGraphicsTextItem to new position ?
I don't really understand what you are asking, but every graphics item inherits void QGraphicsItem::setPos(const QPointF &pos).
Let's say a QGraphicsRectItem is at (x,y) position. When QGraphicsRectItem gets resized, i get new bounding box value. This boundingbox value is given to setRect() of QGraphicsRectItem, to make it larger/smaller. I don't change (x,y) position of the rectangle.
Assume i resize using top-left corner of QGraphicsRectItem. In this case, position of item changes along with resize. Here also, i can set boundingbox value to setRect() function which resize and changes position of item.
I want QGraphicsRectItem setRect() equivalent for QGraphicsTextItem
-
Hi,
There's no direct equivalent. You have setTextWidth that allows you to influence that dimension and the height will be adapted.
If you want more control like "forcing the text within the bound of a particular rectangle, you'll have to implement that yourself.
-
Hi,
There's no direct equivalent. You have setTextWidth that allows you to influence that dimension and the height will be adapted.
If you want more control like "forcing the text within the bound of a particular rectangle, you'll have to implement that yourself.
@SGaist
Hi, I don't expect text to be bound within rectangle. In QGraphicsTextItem bounding-rect 'x' and 'y' value is always 0, 0. I am only able to change bounding-rect width using setTextWidth(). Is there any function in QGraphicsTextItem, QTextDocument or QAbstractTextDocumentLayout which can change bounding-rect x and y values ? -
@SGaist
Hi, I don't expect text to be bound within rectangle. In QGraphicsTextItem bounding-rect 'x' and 'y' value is always 0, 0. I am only able to change bounding-rect width using setTextWidth(). Is there any function in QGraphicsTextItem, QTextDocument or QAbstractTextDocumentLayout which can change bounding-rect x and y values ?@Sridharan said in QGraphicstextitem dynamic resize and changing top-left position of item:
Is there any function in QGraphicsTextItem, QTextDocument or QAbstractTextDocumentLayout which can change bounding-rect x and y values ?
Is that what you question is? Then, yes, subclass
QGraphicsTextItemand override QRectF QGraphicsTextItem::boundingRect() const to return whatever you want. It has always been the case that you can do do this for anyQGraphicsItemif you need to specify a bounding rectangle different from the shape's rectangle. -
@Sridharan said in QGraphicstextitem dynamic resize and changing top-left position of item:
Is there any function in QGraphicsTextItem, QTextDocument or QAbstractTextDocumentLayout which can change bounding-rect x and y values ?
Is that what you question is? Then, yes, subclass
QGraphicsTextItemand override QRectF QGraphicsTextItem::boundingRect() const to return whatever you want. It has always been the case that you can do do this for anyQGraphicsItemif you need to specify a bounding rectangle different from the shape's rectangle.Thanks @JonB. I am able to resize QGraphicsTextItem in all directions by subclass QGraphicsTextItem and override function QRectF QGraphicsTextItem::boundingRect() const
But, on resize, even though boundingbox is large enough to show text but text becomes hidden. Text visibility becomes inconsistent. I started to look into QTextDocument pointer returned from document() function. I think in QTextDocument also may need some changes for bounding box. Is this approach correct or you have any different idea ?