Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Limit size of QGraphicsTextItem?
Qt 6.12 LTS is out! See what's new in the release blog

Limit size of QGraphicsTextItem?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 10.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Is there a way to limit the size (x & y) of a QGraphicsTextItem?

    If the Content/Document is lager than the size of the TextItem it should be simply stripped.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      QGraphicsTextItem automatically adjusts size, so it'll never be smaller than document.
      You can get the size through boundingRect(), and than you can make necessary adjustments to string that you are showing.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Are there any known alternative Classes that one can use instead of QGraphicsTextItem?
        Adjusting the string isn't really an option.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          The string is never adjusted, but only the bounding rectangle. As "Jake007" said, override "QGraphicsTextItem::boundingRect":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicstextitem.html#boundingRect and return any size that you want.
          Also if you are dealing with a simple text, "QGraphicsSimpleTextItem":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicssimpletextitem.html is more appropriate

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Do you mean i should create a new class witch inherits QGraphicsTextItem and write my own boundingRect() witch returns the values i want?

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              [quote author="Artemus" date="1327924598"]Do you mean i should create a new class witch inherits QGraphicsTextItem and write my own boundingRect() witch returns the values i want?[/quote]

              Yes, if you want to limit the bounding rectangle (size).

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                I tried that today and it works, thank you!

                @class QGraphicsTextItemSized : public QGraphicsTextItem
                {
                public:
                QGraphicsTextItemSized(QGraphicsItem* parent = 0);
                QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent = 0);

                QRectF boundingRect() const;
                void   forcePos(qreal x1, qreal y1, qreal x2, qreal y2);
                

                private:
                QRectF forcedSize;
                };@
                @QGraphicsTextItemSized::QGraphicsTextItemSized(QGraphicsItem* parent)
                : QGraphicsTextItem(parent)
                {}

                QGraphicsTextItemSized::QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent)
                : QGraphicsTextItem(text, parent)
                {}

                QRectF QGraphicsTextItemSized::boundingRect() const
                {
                return forcedSize;
                }

                void QGraphicsTextItemSized::forcePos(qreal x1, qreal y1, qreal x2, qreal y2)
                {
                forcedSize.setCoords(0,0,x2-x1,y2-y1);
                setPos(x1,y1);
                }
                @
                @ QString teststring = "Test \n (.Y.) \n Test";
                QGraphicsTextItemSized* text = new QGraphicsTextItemSized();
                scene->addItem(text);
                text->setPlainText(teststring);
                text->forcePos(5,30,115,115);
                text->setTextInteractionFlags(Qt::TextEditorInteraction);@

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  In the above code if i change the boundingrect coordinates to small values total text is not visibling ....can some one please tell me how to adjust the total text with in the bounding rectcordinates....

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    do you added the Constructors?

                    @QGraphicsTextItemSized::QGraphicsTextItemSized(QGraphicsItem* parent)
                    : QGraphicsTextItem(parent)
                    {}

                    QGraphicsTextItemSized::QGraphicsTextItemSized(const QString& text, QGraphicsItem* parent)
                    : QGraphicsTextItem(text, parent)
                    {}@

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      yes i added..i want to fix it exactly with in the bounding rect...that is boundingrect width will be equal to textwidth...i tried like this...

                      @

                      GraphicsTextItemSized::GraphicsTextItemSized(QGraphicsItem* parent): QGraphicsTextItem(parent){}

                      GraphicsTextItemSized::GraphicsTextItemSized(const QString &text, QGraphicsItem *parent):QGraphicsTextItem(text,parent)
                      {
                      setPlainText( text);
                      }
                      QRectF GraphicsTextItemSized::boundingRect() const
                      {
                      return (adjustedrect);
                      }

                      void GraphicsTextItemSized::forcedsize(qreal x1,qreal y1,qreal x2,qreal y2)
                      {
                      adjustedrect.setCoords(0,0,x2-x1,y2-y1);
                      }

                      @

                      @
                      QGraphicsRectItem *rect = new QGraphicsRectItem(0,0,0,0);
                      GraphicsTextItemSized *test = new GraphicsTextItemSized(QString("vcc"),rect);
                      test->forcedsize(0,0,10,10);
                      test->setPos(10.0,10.0);
                      scene->addItem(rect);
                      @

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        [quote author="mahesh438" date="1328327702"] @(QStrin("vcc"),rect);@ [/quote]

                        -> QStrin g ("vcc") ?

                        I can't see any other mistake.

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          i got one doubt wheather we have to use any QFontmatrics to adjust the size of the string to fit in the bounding rect

                          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
                          • Unsolved