0

I have a scene with QGraphicsPixmapItem, and show a QGraphicScene object like a rectangle.

    def setupUI(self):
        self.pixmap01 = QPixmap.fromImage(qImg)
        self.mainpic = QGraphicsPixmapItem(self.pixmap01)
        self.scene1.addItem(self.mainpic)
        self.graphicsViewScene1.setScene(self.scene1)
        self.scene1.update()

    def DrawRectangle(self):
        scene = RectangleScene()
        scene.addItem(self.mainpic)
        self.graphicsViewScene1.setScene(scene)

Now i a draw rectangle and top of a scene(this part of code is fine) but after right click on rect and choose replace pic item(option menu) i want to replace this selected area of QPixmap with another QPixmap(for this action i use Qpainter but nothing happens)

class RectangleScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super(RectangleScene, self).__init__(*args, **kwargs)

    def mousePressEvent(self, event):
        self.clean_scene()
        self.start_point = event.scenePos()
        self.end_point =self.start_point
        self.graphics_line = QGraphicsRectItem(QRectF(self.start_point, self.end_point))
        self.update_path()

    def mouseMoveEvent(self, event):
        self.end_point = event.scenePos()
        self.update_path()

    def mouseReleaseEvent(self, event):
        self.end_point = event.scenePos()
        self.update_path()

    def update_path(self):     
        self.graphics_line.setRect(QRectF(self.start_point, self.end_point))
        self.addItem(self.graphics_line)

    def contextMenuEvent(self, event):
        menu = QtWidgets.QMenu()
        f1 = menu.addAction("repalce pic")
        if action == f1: 
           tmp_rect = QRectF(self.start_point, self.end_point)
           new_pix_map = QPixmap.fromImage(qImg)
           painter = QPainter(self.imageProcessing.pixmap01)
           painter.drawPixmap(tmp_rect,new_pix_map ,tmp_rect)

in another part of code after select this area, get a ndarray of this area and add some filters then i want to add top of this rect

draw a rectangle

md.119
  • 29
  • 7
  • No, that part of code is *not* fine: you should not try to add an item to a scene multiple times (run your code in a terminal or prompt and see the output). That said, it's completely unclear how you expect to add/update the contents of that pixmap: what if the rectangle is *outside* the pixmap boundaries? Also, you're just updating the *reference copy* to that pixmap, but not updating the pixmap of the item itself. Besides, please, separate your functions with blank lines, your code is very annoying to read and understand. – musicamante Feb 15 '23 at 05:27
  • Thanks @musicamante, that's another problem i have, but for now i stuck in this part of code – md.119 Feb 15 '23 at 05:46
  • If you want to replace the pixmap of an existing QGraphicsPixmapItem, then just call `setPixmap()` again. But I sincerely doubt that it would actually solve your issue, since that will constantly paint *over* the previous pixmap, meaning that you'll probably get multiple (parts of) rectangles. You need to understand how painting works and create a possible "stack" of drawings for items. But that would be completely up to you, as it would be too broad as a question for StackOverflow, unless you provide a valid MRE based on your actual attempts in trying to achieve that result. – musicamante Feb 15 '23 at 05:51
  • i want to add new pixmap on top of main pixmap and also can remove when draw a new rectangle – md.119 Feb 15 '23 at 05:57
  • I am aware of what you want to do, and the fact remains: whenever you update the pixmap, changes will *overlay* on it, and that's probably *not* what you want; that's why I specifically told you about using a *stack* of drawings. That's how common imaging programs work: they don't actually paint over the source image, otherwise it would be like painting over a physical canvas: whenever you paint something new, it is drawn **over** the previous content; they create an abstract stack of objects that can be eventually changed or discarded. It's up to you to implement how that stack would work. – musicamante Feb 15 '23 at 06:08
  • thanks for your advice, can you(@musicamante) help me for apply this method or provide a sample link – md.119 Feb 15 '23 at 06:17
  • Sorry, but no: as said, that's completely up to you, as there are thousands of ways to do that and only you can know what works best for your needs. Take your time to consider the above aspect and try to implement it on your own, then if you have issues with that, create an appropriate answer that shows your attempts including a related [mre]. – musicamante Feb 15 '23 at 06:20

0 Answers0