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. [SOLVED] How to have 1+ QGraphicsTextItem in a class
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to have 1+ QGraphicsTextItem in a class

Scheduled Pinned Locked Moved General and Desktop
qgraphicstextitbegginer
8 Posts 4 Posters 2.4k Views 4 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.
  • N Offline
    N Offline
    nanoandrew4
    wrote on 20 Jul 2015, 11:18 last edited by nanoandrew4
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jul 2015, 11:27 last edited by
      #2

      Hi,

      Do you mean something like QVector< QGraphicsTextItem *> _textItems; ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply 22 Jul 2015, 11:42
      0
      • S SGaist
        20 Jul 2015, 11:27

        Hi,

        Do you mean something like QVector< QGraphicsTextItem *> _textItems; ?

        N Offline
        N Offline
        nanoandrew4
        wrote on 22 Jul 2015, 11:42 last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on 22 Jul 2015, 12:44 last edited by
          #4

          Create your own QGraphicsItem subclass
          Two options:

          1. Paint all the text you need yourself in the paint event
          2. 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)
          N 1 Reply Last reply 22 Jul 2015, 18:25
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 22 Jul 2015, 12:56 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Asperamanca
              22 Jul 2015, 12:44

              Create your own QGraphicsItem subclass
              Two options:

              1. Paint all the text you need yourself in the paint event
              2. 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)
              N Offline
              N Offline
              nanoandrew4
              wrote on 22 Jul 2015, 18:25 last edited by
              #6

              @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!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 22 Jul 2015, 21:44 last edited by
                #7

                @mrjj said:
                Have a look at
                http://www.bogotobogo.com/Qt/Qt5_QGraphicsView_QGraphicsScene_QGraphicsItems.php

                What 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.

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on 23 Jul 2015, 07:58 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  8/8

                  23 Jul 2015, 07:58

                  • Login

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