QGraphicsTextItem doesn't show up
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!
Re: QGraphicsTextItem doesn't show up
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.
Re: QGraphicsTextItem doesn't show up
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.
Re: QGraphicsTextItem doesn't show up
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
Re: QGraphicsTextItem doesn't show up
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?
Re: QGraphicsTextItem doesn't show up
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.
Re: QGraphicsTextItem doesn't show up
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?