Home > database >  Only part of my QGraphicsTextItem is shown in my scene. To solve the problem i want to change the si
Only part of my QGraphicsTextItem is shown in my scene. To solve the problem i want to change the si

Time:07-08

As described above I have a scene with several items on it in which I want to add a QgraphicstextItem. Of that item only see the first few lines of pixels no matter where on the scene I put it (See Picture). As there is no other object in front of the QGarhicsTextItem the only reason I can think of for it not being shown completely is that the boundingrect is too small. I checked the size of it and its width is only 8. Why does my boundingRect not change its size? Main.cpp

RandomNumber* randNum = new RandemNumber;
randNum->setPlainText("Hello"); 
randNum->setPostion(2100,2100);
randNum->boundingRect().adjust(2000,2000,2100,2100);
sce[n][1]e->addItem(randNum);
qDebug() << randNum->boundingRect().width;

My RandomNumber.cpp

    prepareGeometryChange();
    boundingRect().adjust(2000,2000,2100,2100);
    qDebug() << boundingRect().width;

Neither works. Both qDebugs return 8 as the width. Thanks for your Help

enter image description here

CodePudding user response:

Unfortunately, OP didn't provide an GIF Animation of testQGraphicsViewQGraphicsTextItem

The quite long "Lorem ipsum" sample text doesn't fit in but is shown. (Please, note the scrollbar which is added.)

Afterwards, I un-commented the pQGTextItem->adjustSize();.

Output:

GIF Animation of testQGraphicsViewQGraphicsTextItem (with pQGTextItem->adjustSize())

TL;DR:

CodePudding user response:

randNum->boundingRect().adjust(2000,2000,2100,2100); and boundingRect().adjust(2000,2000,2100,2100); are pointless, you modify temporary copy of the object returned from the function. Notice that boundingRect() is defined as const - you can not use it to modify internal state of your object.

adjustSize should work, unless you overriden boundingRect() method, which is not neccessary.

Maybe you dont even need RandomNumber class, you can just use QGraphicsTextItem.

  • Related