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. QGraphics and Text
Forum Updated to NodeBB v4.3 + New Features

QGraphics and Text

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 821 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.
  • S Offline
    S Offline
    sevi78
    wrote on last edited by
    #1

    Hi,

    i am new to QT and try to learn it. So i would like to create a widget that holds a QGraphicsItem and some text fields.

    I createt a scene and add the QGraphicsPixmapItem, wich should itself hold a text item.
    but the problem is, the text(QLabel) is in a seperate window insteat of the scene...

    import sys
    from PyQt5.QtCore import (
        Qt,
        QBasicTimer
    )
    from PyQt5.QtGui import (
        QBrush,
        QPixmap
    )
    from PyQt5.QtWidgets import (
        QApplication,
        QGraphicsItem,
        QGraphicsPixmapItem,
        QGraphicsRectItem,
        QGraphicsScene,
        QGraphicsView, 
        QLineEdit, 
        QLabel
    )
    
    SCREEN_WIDTH            = 800
    SCREEN_HEIGHT           = 600
    FRAME_TIME_MS           = 16  # ms/frame
    
    class Node(QGraphicsPixmapItem):
        def __init__(self, offset_x, offset_y, parent = None):
            QGraphicsPixmapItem.__init__(self,parent)
            self.setPixmap(QPixmap("bullet.png"))
            self.offset_x = offset_x
            self.offset_y = offset_y
            self.active = True
            self.id = 0
            self.frames = 0
            
            self.text = QLabel()
            self.text.parent = self
            self.text.move(self.offset_x, self.offset_y)
            self.text.setText(str(self.id))
            self.text.setVisible(True)
    
        def game_update(self):
            pass
    
    class Scene(QGraphicsScene):
        def __init__(self, parent = None):
            QGraphicsScene.__init__(self, parent)
    
            # use a timer to get 60Hz refresh (hopefully)
            self.timer = QBasicTimer()
            self.timer.start(FRAME_TIME_MS, self)
    
            bg = QGraphicsRectItem()
            bg.setRect(-1,-1,SCREEN_WIDTH+2,SCREEN_HEIGHT+2)
            bg.setBrush(QBrush(Qt.black))
            self.addItem(bg)
    
            self.bullets = [Node(100,100),
                            Node(200,200)]
            id = 0
            for b in self.bullets:
                b.setPos(b.offset_x, b.offset_y)
                b.id = id
                id += 1
                self.addItem(b)
    
            self.view = QGraphicsView(self)
            self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.view.show()
            self.view.setFixedSize(SCREEN_WIDTH,SCREEN_HEIGHT)
            self.setSceneRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)
    
    
        def timerEvent(self, event):
            self.game_update()
            self.update()
    
        def keyPressEvent(self, event):
            if event.key() == Qt.Key_Escape:
                # Quit the program if the user presses the "esc" key
                QApplication.quit()
    
        def game_update(self):
            pass
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        scene = Scene()
        sys.exit(app.exec_())
    
    

    maybe someone can help me out?

    JonBJ 1 Reply Last reply
    0
    • S sevi78

      Hi,

      i am new to QT and try to learn it. So i would like to create a widget that holds a QGraphicsItem and some text fields.

      I createt a scene and add the QGraphicsPixmapItem, wich should itself hold a text item.
      but the problem is, the text(QLabel) is in a seperate window insteat of the scene...

      import sys
      from PyQt5.QtCore import (
          Qt,
          QBasicTimer
      )
      from PyQt5.QtGui import (
          QBrush,
          QPixmap
      )
      from PyQt5.QtWidgets import (
          QApplication,
          QGraphicsItem,
          QGraphicsPixmapItem,
          QGraphicsRectItem,
          QGraphicsScene,
          QGraphicsView, 
          QLineEdit, 
          QLabel
      )
      
      SCREEN_WIDTH            = 800
      SCREEN_HEIGHT           = 600
      FRAME_TIME_MS           = 16  # ms/frame
      
      class Node(QGraphicsPixmapItem):
          def __init__(self, offset_x, offset_y, parent = None):
              QGraphicsPixmapItem.__init__(self,parent)
              self.setPixmap(QPixmap("bullet.png"))
              self.offset_x = offset_x
              self.offset_y = offset_y
              self.active = True
              self.id = 0
              self.frames = 0
              
              self.text = QLabel()
              self.text.parent = self
              self.text.move(self.offset_x, self.offset_y)
              self.text.setText(str(self.id))
              self.text.setVisible(True)
      
          def game_update(self):
              pass
      
      class Scene(QGraphicsScene):
          def __init__(self, parent = None):
              QGraphicsScene.__init__(self, parent)
      
              # use a timer to get 60Hz refresh (hopefully)
              self.timer = QBasicTimer()
              self.timer.start(FRAME_TIME_MS, self)
      
              bg = QGraphicsRectItem()
              bg.setRect(-1,-1,SCREEN_WIDTH+2,SCREEN_HEIGHT+2)
              bg.setBrush(QBrush(Qt.black))
              self.addItem(bg)
      
              self.bullets = [Node(100,100),
                              Node(200,200)]
              id = 0
              for b in self.bullets:
                  b.setPos(b.offset_x, b.offset_y)
                  b.id = id
                  id += 1
                  self.addItem(b)
      
              self.view = QGraphicsView(self)
              self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              self.view.show()
              self.view.setFixedSize(SCREEN_WIDTH,SCREEN_HEIGHT)
              self.setSceneRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)
      
      
          def timerEvent(self, event):
              self.game_update()
              self.update()
      
          def keyPressEvent(self, event):
              if event.key() == Qt.Key_Escape:
                  # Quit the program if the user presses the "esc" key
                  QApplication.quit()
      
          def game_update(self):
              pass
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          scene = Scene()
          sys.exit(app.exec_())
      
      

      maybe someone can help me out?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @sevi78
      You cannot add a QLabel onto a QGraphicsPixmapItem. If anything you might use a QGraphicsTextItem for text on a graphics scene. Even then I don't know if you can just put the text on top of the pixmap. You might be able to handle them with a QGraphicsItemGroup, but there may be a better way.

      Also, you cannot "create a widget that holds a QGraphicsItem and some text fields." because QGraphicsItems don't live on widgets.

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @sevi78
        You cannot add a QLabel onto a QGraphicsPixmapItem. If anything you might use a QGraphicsTextItem for text on a graphics scene. Even then I don't know if you can just put the text on top of the pixmap. You might be able to handle them with a QGraphicsItemGroup, but there may be a better way.

        Also, you cannot "create a widget that holds a QGraphicsItem and some text fields." because QGraphicsItems don't live on widgets.

        S Offline
        S Offline
        sevi78
        wrote on last edited by
        #3

        @JonB
        Thanks for reply, that helps me out, knowing now, its the wrong apporach.
        I might use a Qwidget that stores a graphic item and a label and put that into a QWindow ? maybe :)

        JonBJ 1 Reply Last reply
        0
        • S sevi78

          @JonB
          Thanks for reply, that helps me out, knowing now, its the wrong apporach.
          I might use a Qwidget that stores a graphic item and a label and put that into a QWindow ? maybe :)

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @sevi78
          You won't want graphics items on widgets. Maybe you should say/show just what you are trying to achieve, others may know better than I. Are you trying to get text with a pixmap background? Maybe you can do that with QLabel, or maybe with a stylesheet for a background image? Maybe you want an item delegate....

          1 Reply Last reply
          1
          • C Offline
            C Offline
            CPPUIX
            wrote on last edited by
            #5

            @sevi78 You should mention what is your end goal, it would help us a lot to suggest the best ( possibly opinion based) approach for it.
            And just to clarify, by end goal I mean what do you want your application to look like and do?

            S 1 Reply Last reply
            0
            • C CPPUIX

              @sevi78 You should mention what is your end goal, it would help us a lot to suggest the best ( possibly opinion based) approach for it.
              And just to clarify, by end goal I mean what do you want your application to look like and do?

              S Offline
              S Offline
              sevi78
              wrote on last edited by
              #6

              @Abderrahmene_Rayene

              good point.
              ok. i try to explain what my goal is.
              I try to create a Game like PAXGALAXIA.
              I used BlenderGameEngine (bge) so far, but bge has no UI features.
              And programming a whole UI Framework like QT or TK or similar, is way too complicated for me.. i already did it once, more or less successfull. It needs a lot of skills and experience to create a UI Framework, that works smooth.
              So i decided to learn QT, create the GUI and implement the logics there.

              There the code looks something like this:

              import bge
              
              scene = bge.getCurrentScene()
              
              def main(): 
              	# add a plane that holds graphic
              	plane = scene.addObject("Plane")
              	
              	# give the plane a variable
              	plane["value"] = 0
              	
              	# move the Plane to it desired Position x,y,z
              	plane.worldPosition = (x,y,z)
              	
              	# add a TextLabel
              	text = scene.addObject("Text")
              	
              	# put text to the same position as its parent
              	text.worldPosition = plane.worldPosition
              	
              	# parent it to the Plane to make shure it is always
                      # on the same position as its parent, 
                      # if the parent gets moved
              	
              	text.setParent(plane)
              	
              	# display the variable "value" on the text label
              	text.text = label["value"]
              

              now i try translate this code as a start, this would give me the understanding how the workflow in QT is. Hope it helps for better undertanding.
              And anyway, thanks for your immediate help! its awesome, got many replies to my question in short time, really great forum here !

              C 1 Reply Last reply
              0
              • S sevi78

                @Abderrahmene_Rayene

                good point.
                ok. i try to explain what my goal is.
                I try to create a Game like PAXGALAXIA.
                I used BlenderGameEngine (bge) so far, but bge has no UI features.
                And programming a whole UI Framework like QT or TK or similar, is way too complicated for me.. i already did it once, more or less successfull. It needs a lot of skills and experience to create a UI Framework, that works smooth.
                So i decided to learn QT, create the GUI and implement the logics there.

                There the code looks something like this:

                import bge
                
                scene = bge.getCurrentScene()
                
                def main(): 
                	# add a plane that holds graphic
                	plane = scene.addObject("Plane")
                	
                	# give the plane a variable
                	plane["value"] = 0
                	
                	# move the Plane to it desired Position x,y,z
                	plane.worldPosition = (x,y,z)
                	
                	# add a TextLabel
                	text = scene.addObject("Text")
                	
                	# put text to the same position as its parent
                	text.worldPosition = plane.worldPosition
                	
                	# parent it to the Plane to make shure it is always
                        # on the same position as its parent, 
                        # if the parent gets moved
                	
                	text.setParent(plane)
                	
                	# display the variable "value" on the text label
                	text.text = label["value"]
                

                now i try translate this code as a start, this would give me the understanding how the workflow in QT is. Hope it helps for better undertanding.
                And anyway, thanks for your immediate help! its awesome, got many replies to my question in short time, really great forum here !

                C Offline
                C Offline
                CPPUIX
                wrote on last edited by
                #7

                @sevi78 I found something interesting when I searched for qt game development.

                Qt Game Engine (QGE)

                It's a bit old but it might give you ideas even if it does not work for you.

                here's the link:

                Qt Game Engine

                You should also check out games that were made with Qt.

                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