Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicstextitem dynamic resize and changing top-left position of item
Qt 6.11 is out! See what's new in the release blog

QGraphicstextitem dynamic resize and changing top-left position of item

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sridharan
    wrote on last edited by Sridharan
    #1

    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());
        }
    }
    

    };

    JonBJ 1 Reply Last reply
    0
    • S Sridharan

      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());
          }
      }
      

      };

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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).

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @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).

        S Offline
        S Offline
        Sridharan
        wrote on last edited by Sridharan
        #3

        @JonB

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            S Offline
            S Offline
            Sridharan
            wrote on last edited by
            #5

            @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 ?

            JonBJ 1 Reply Last reply
            0
            • S Sridharan

              @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 ?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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 QGraphicsTextItem and override QRectF QGraphicsTextItem::boundingRect() const to return whatever you want. It has always been the case that you can do do this for any QGraphicsItem if you need to specify a bounding rectangle different from the shape's rectangle.

              S 1 Reply Last reply
              0
              • JonBJ JonB

                @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 QGraphicsTextItem and override QRectF QGraphicsTextItem::boundingRect() const to return whatever you want. It has always been the case that you can do do this for any QGraphicsItem if you need to specify a bounding rectangle different from the shape's rectangle.

                S Offline
                S Offline
                Sridharan
                wrote on last edited by Sridharan
                #7

                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 ?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved