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. Fixed Text Width in QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Fixed Text Width in QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.0k Views
  • 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.
  • keksi venksiK Offline
    keksi venksiK Offline
    keksi venksi
    wrote on last edited by
    #1

    Hi all,

    Im trying to draw a text with fixed width in a QGraphicsScene....
    I tried with all the few options like painter.drawText() and painter.drawPath() ..
    With this options i can able to draw the text in a QGraphicsScene but the text seems to be in expanding state when we zoom In and in collapsing state when we zoom out .
    What i want is , when ever I zoom in or out the text should have a fixed width from the beginning stage .
    SUGGESTIONS WILL BE HIGHLY APPRECIATED

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SchrodingersGat
      wrote on last edited by
      #2

      Sounds like you want to set the QGraphicsItem::ItemIgnoresTransformations attribute?

      See http://doc.qt.io/qt-5/qgraphicsitem.html#setFlags

      keksi venksiK 1 Reply Last reply
      0
      • S SchrodingersGat

        Sounds like you want to set the QGraphicsItem::ItemIgnoresTransformations attribute?

        See http://doc.qt.io/qt-5/qgraphicsitem.html#setFlags

        keksi venksiK Offline
        keksi venksiK Offline
        keksi venksi
        wrote on last edited by
        #3

        @SchrodingersGat

        Hi...
        Thanks for your reply ...

        Hope its a good solution ..

        The problem in my case is that in my app there are N no of items already present in the QGraphicsScene . Also according to the requirements , I should draw the text seperately with out disturbing the items present in the scene .
        So I drawn the text in drawBackground() method of my scene .... Want some help for drawing the text through painter

        1 Reply Last reply
        0
        • m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #4

          Hi,
          when you zoom, the paint device's transform is probably changed before it enters the drawBackground virtual function. So probably you can save the transform, reset it, then draw your text and at last restore the transform for use in the other virtual functions, like:

          painter->save();
          painter->setTransform(QTransform());
          //draw text
          painter->restore();
          

          -Michael.

          1 Reply Last reply
          0
          • BuckwheatB Offline
            BuckwheatB Offline
            Buckwheat
            wrote on last edited by
            #5

            Hi,
            I don't think ignoring is a good solution as it will ignore translations and rotations too and lose any object associations that are from the beginning.

            I have found, you can use inverse scaling on the object. Take a snapshot of the transformations scale when you start. From then after, apply an inverse scale on the object using save and restore like @m-sue presented. You will most likely have to scale "about" the location to apply the inverse scale.

            Another idea is to map the text location at the current transformation to the screen, setup the transformation as @m-sue suggested and then draw the text at the screen location.

            Both these suggestions should help you move the text around. If you do not want to move the text (ie a Title, Legend, etc) then the QGraphicsScene::drawForeground (...) and QGraphicsScene::drawBackground (...) will suffice with a cleared transformation.

            Dave Fileccia

            keksi venksiK 1 Reply Last reply
            0
            • BuckwheatB Buckwheat

              Hi,
              I don't think ignoring is a good solution as it will ignore translations and rotations too and lose any object associations that are from the beginning.

              I have found, you can use inverse scaling on the object. Take a snapshot of the transformations scale when you start. From then after, apply an inverse scale on the object using save and restore like @m-sue presented. You will most likely have to scale "about" the location to apply the inverse scale.

              Another idea is to map the text location at the current transformation to the screen, setup the transformation as @m-sue suggested and then draw the text at the screen location.

              Both these suggestions should help you move the text around. If you do not want to move the text (ie a Title, Legend, etc) then the QGraphicsScene::drawForeground (...) and QGraphicsScene::drawBackground (...) will suffice with a cleared transformation.

              keksi venksiK Offline
              keksi venksiK Offline
              keksi venksi
              wrote on last edited by
              #6

              @Buckwheat @m-sue

              Thanks for your reply ...

              @Buckwheat

              "I have found, you can use inverse scaling on the object. Take a snapshot of the transformations scale when you start. From then after, apply an inverse scale on the object using save and restore like @m-sue presented. You will most likely have to scale "about" the location to apply the inverse scale."

              Can you explain this point with some codes Please

              1 Reply Last reply
              0
              • BuckwheatB Offline
                BuckwheatB Offline
                Buckwheat
                wrote on last edited by
                #7

                @keksi-venksi ... I cannot give you exact code for proprietary reasons but here is the general idea of what I am saying...

                painter->save ()

                QTransform trans = painter->transform ();
                double currentScale = extractScale (trans); // Write function to extract the scale portion of the transform
                double newScale = originalScale / currentScale;
                trans.scale (newScale, newScale); // This may need to be scaled about the origin of the object so the text
                // stays with the object
                // eg. scaleAbout (invScale, originPt);
                painter->setTransform (trans); // Now painter has a transform that has the scale removed
                // TODO: draw your text
                painter->restore ();

                Scaling about points can be found in Computer Graphics Principles and Practice by Foley,VanDam,Feiner, and Hughes pages 208-210.

                Since transformations are orthogonal, scales are found along the diagonal. If rotations are involved, the scales are componentized,

                Dave Fileccia

                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