[SOLVED] How to have 1+ QGraphicsTextItem in a class
-
So I have been following a tutorial online to make a game on YouTube (https://www.youtube.com/watch?v=8ntEQpg7gck&list=PLMgDVIa0Pg8WrI9WmZR09xAbfXyfkqKWy) and when he adds the health and score he does it in separate classes, instead of all in one class. I was thinking of using pointers, but have had no success. I am looking for a way to add more than one QGraphicsTextItem to the same class so I don't have to have overflow of classes with 5 lines.
-
Hi,
Do you mean something like
QVector< QGraphicsTextItem *> _textItems;
? -
@SGaist Basically what I want is to be able to display speed, rotation, wind etc on the screen using one class instead of many. The example I saw in the tutorial used one class for health and one for score, but I want to have them all in one class. If what SGaist suggested will work for this, could someone explain how to use that example in my case, briefly? I'm fairly new to Qt.
-
Create your own QGraphicsItem subclass
Two options:- Paint all the text you need yourself in the paint event
- Paint nothing, instead create single QGraphicsTextItems as children of your class (which, btw are fat and slow, consider QGraphicsSimpleTextItem unless you need fancy formatting or editing)
-
If you can accept all in one line, you can have more values pr TextItem in a sense if you do
setPlainText ( QString ( "Score: %1 Speeds %2" ).arg ( score ).arg ( windspeed ) );But as Asperamanca says, subclassing QGraphicsItem and paint it as you want is way more flexible.
-
@Asperamanca Anywhere I could find help with that to get started? If you can think of something that could help I would appreciate if you could post it, but I will start looking for help with the issue. Thank you for your answer!
-
@mrjj said:
Have a look at
http://www.bogotobogo.com/Qt/Qt5_QGraphicsView_QGraphicsScene_QGraphicsItems.phpWhat you want is then in the Paint function to draw the different texts you need
using painter->drawText(...)Please note that it be affected by Transformations to the view. So might need to fiddle with font size etc.
-
You can take a look at the "Chip" class in the 40000 chips example. Or the classes in the Drag Drop Robot example. They don't use text, but the principles are the same.