QGraphics and Text
-
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?
-
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?
@sevi78
You cannot add aQLabelonto aQGraphicsPixmapItem. If anything you might use aQGraphicsTextItemfor 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 aQGraphicsItemGroup, 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. -
@sevi78
You cannot add aQLabelonto aQGraphicsPixmapItem. If anything you might use aQGraphicsTextItemfor 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 aQGraphicsItemGroup, 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. -
@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 :)@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 withQLabel, or maybe with a stylesheet for a background image? Maybe you want an item delegate.... -
@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?@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 ! -
@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 !@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:
You should also check out games that were made with Qt.