Fixed Text Width in QGraphicsScene



  • 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



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

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



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



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



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



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



  • @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,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.