How to write text to the screen



  • I wanted to write the score in a game of pong. So I wanted the string "Score: " printed centered at the top and bottom of the screen, followed by the integer corresponding to the player's score. How would I do this?



  • Can you add a bit of context, please? Are you doing custom drawing for your widgets or what?



  • QLabel::setText("Score: " + score_as_intOrLong);
    (or something like this...)



  • You can also use QPainter's drawText().



  • [quote author="peppe" date="1323238729"]Can you add a bit of context, please? Are you doing custom drawing for your widgets or what?[/quote]

    The way I wanted to be able to do it was to basically have a text object that I could manipulate the same way I would, say, a rectangle object or an ellipse object. Basically something that I could easily resize and move around. Does that help at all?



  • So... are you using the Graphics View framework then? If so, you can use [[doc:QGrapicsTextItem]].



  • [quote author="Andre" date="1323285355"]So... are you using the Graphics View framework then? If so, you can use [[doc:QGrapicsTextItem]].[/quote]

    That seems to work, but now I have another issue. If I change what's in the item, by using setPlainText(QString), it doesn't remove what was already on the screen. It just adds it at the end of what was already there (I only call addItem(QGraphicsTextItem) once). How would I make it update?



  • Nevermind. I was converting integers to strings using a stringstream and forgot to clear it afterwards. I think I've got it working.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.