Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to write text to the screen
Qt 6.11 is out! See what's new in the release blog

How to write text to the screen

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tactical_Sandbags
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

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

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • B Offline
        B Offline
        broadpeak
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • V Offline
          V Offline
          veeeee_d
          wrote on last edited by
          #4

          You can also use QPainter's drawText().

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tactical_Sandbags
            wrote on last edited by
            #5

            [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?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tactical_Sandbags
                wrote on last edited by
                #7

                [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?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tactical_Sandbags
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved