PDA

View Full Version : QGraphicsTextItem doesn't show up



chapu
11th July 2010, 06:41
I have a QGraphicsScene to show some data points. Each data point is an QGraphicsItem object and added to the scene with QGraphicsScene::addItem() function. All data points are shown OK.

Then I want to add a text to the same scene. I use QGraphicsTextItem and the code looks something like:

QGraphicsTextItem *text = new QGraphicsTextItem("Data");
myScene->addItem(text);

But the text does not show up! Do I miss some settings for QGraphicsTextItem here? I checked some code examples on the forum and it looked as simple as above. Why mine does not work?

Also, if I want to put the text on a location on the scene, should I use QQGraphicsItem::setPos(x,y), where x and y are in scene coodinates?

Thanks a lot for any help!

Lykurg
11th July 2010, 06:48
Your code is fine, also use setPos() for arranging your text item. I can't think of any reason, why your text is not shown, but I guess the "error" in somewhere in your surrounding code. So please check in a simple example, if you can insert texts to a scene. If so, search your other code for errors.

chapu
11th July 2010, 14:20
I ran a standalone simple example with QGraphicsTextItem and it works fine. So I believe there is something in my application code that caused the problem.

When I added other QGraphicsItem's to the my scene (such as QGraphicsLineItem), all worked OK. But only QGraphicsTextItem does not! Any difference between QGraphicsLineItem and QGraphicsTextItem? I just do not understand why.

Lykurg
11th July 2010, 14:29
No, no difference. Can you show some code?

Possible issues I can think of:
Item is outside the visible area
background color and your font color are the same
If you use a custom font, it is maybe not found

chapu
11th July 2010, 14:47
I did not change any setting for QGraphicsTextItem, just simply create it and add it to the scene like this:

QGraphicsTextItem *text = new QGraphicsTextItem("Data");
myScene->addItem(text);

One thing I can guess is that the text is drawn out of visible area. How do I control the area or the boundary of the text?

Lykurg
11th July 2010, 14:59
after you have added the item call setSceneRect() with QGraphicsScene::itemsBoundingRect(), then you should scroll to each item.

EDIT: It it is maybe hidden by another item? Try to "play" with the z value of the item.

chapu
12th July 2010, 00:48
Thanks, Lykurg. I tried calling QGraphicsScene::itemsBoundingRect() and QGraphicsScene::sceneRect() where there is only an QGraphicsTextItem added to the scene. It turned out that the items bounding rect is much larger than the scene rect. That is why the text item is not shown on the scene.

I do not know why the default QGraphicsTextItem has such larger bounding rect. How can I make it smaller to fit within the scene rect?