Solved How to add an image at a specific area in QGraphicsScene
-
Hi,
I have an large image and i want to add this image to a specific area (in a rectange that i can enter the x,y,width and height) in QGraphicsScene. How can i do that ?
-
@AdemMetinCALI
I have never used graphics stuff, but wouldn't you do this on theQGrapicsItem
by calling https://doc.qt.io/qt-5/qgraphicsitem.html#setPos? -
You can use QGraphicsPixmapItem for this.
QPixmap::scaled to scale the image (shrink, expand the image, then move whole item to given point (x, y) )
-
@JonB Thanks for your advice, but i cannot find any member functions or properties that solve my problem and i think that QGraphicsPixmapItem is more convenient for me.
-
@AdemMetinCALI Yes, now that @Pl45m4 posted that specific
QGraphicsPixmapItem
for your case it is best to use. -
@Pl45m4 said in How to add an image at a specific area in QGraphicsScene:
QPixmap::scaled to scale the
Thanks for your advice, but i don't want to shrink the image. I want to display a portion of the image in specific area.
-
@AdemMetinCALI
Then I think you should read https://forum.qt.io/topic/83380/how-to-partially-draw-a-qgraphicspixmapitem. Which is going to tell you not to use aQGraphicsPixmapItem
after all, but a customQGraphicsItem
instead! -
Hi,
If you want only a portion of your image, one way to do it is to load the original in a QImage and then use the copy method to retrieve only the part you want to show and make a QGraphicsPixmap from that.
-
@SGaist Thanks for your suggestion, but i think your solution causes performance issues. I try to build a hud and it has many symbology. How can i deal with the performance issues?
-
Then exploit the fact that you can use the sceneRect property to only show the part you want from the picture.
-
@SGaist Thanks for your suggestion, but i want to add more picture in different areas of the scene. With the sceneRect how can i do that?
-
@AdemMetinCALI said in How to add an image at a specific area in QGraphicsScene:
i want to add more picture in different areas of the scene.
I'm trying to understand your requirements. Could you please clarify:
- How many "large images" are you using? Do you use the same "large image" all the time, or do you keep loading a different "large image"
- Are you trying to cut the large image into smaller pieces, and draw each small piece in a different part of the scene?
If you answer "yes" to #2, that means you want to make sprites. See https://evileg.com/en/post/92/ for example.
i think your solution causes performance issues. I try to build a hud and it has many symbology. How can i deal with the performance issues?
Why do you think there are performance issues? How serious are they?
-
I solved my problem in 2 stpes: In the first step, I've created a new class derived from QWidget, add the image into this widget and i made this widget transparent. In the second step, i added this widget into scene.