I have a QT-project (using C++) where instances of a certain user-defined QGraphicsItem
called Person
move around the scene. Sometimes those Persons
interact so that some of them change color.
Now I want to put a text field in the window and display counts of how many I have of each color. But since the change occurs within the call to the Person::advance
-method I want to create a text field that can be updated from within these.
I could easily display some text by adding the following code to my main.cpp:
QGraphicsSimpleTextItem *text1 = new QGraphicsSimpleTextItem;
text1->setPos(-200, -150);
text1->setText("This is an arbitrary English sentence");
scene.addItem(text1);
but I do not know how to access and alter the text of this variable text1
from within the advance
-method of the Persons
in my scene. What is a good strategy for this?
Should I create a global variable keeping track of the count, and if I do, how can I then update the text field? Or should the text not even be on my
QGraphicsScene
, but rather be defined in some other more appropriate place where it is callable from everywhere in the program? Is there a generic way of doing this?