QPixmap not updating in QGraphicsScene after using QPainter on it - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: QPixmap not updating in QGraphicsScene after using QPainter on it (/thread-36910.html) |
QPixmap not updating in QGraphicsScene after using QPainter on it - Leo_Red - Apr-11-2022 I created a custom widget modifies QPixmap which just adds QPianter drawing function to the widget. from PySide2.QtGui import QPixmap from PySide2.QtGui import QPainter from ui import painter class Surface(QPixmap): def __init__(self, *args): super().__init__(*args) self.Painter = QPainter(self) def drawRect(self,x,y,w,h): self.Painter.drawRect(x,y,w,h) def drawEllipse(self,x,y,w,h): self.Painter.drawEllipse(x,y,w,h) def drawLine(self,x1,y1,x2,y2): self.Painter.drawLine(x1,y1,x2,y2)Here is the main.py (for testing) from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem from PySide2.QtGui import QPixmap, QPainter from ui import painter from Surface import Surface class Painter(QMainWindow): def __init__(self): super().__init__() self.ui = painter.Ui_MainWindow() self.ui.setupUi(self) self.scene = QGraphicsScene() self.ui.graphicsView.setScene(self.scene) self.pix = Surface('/home/midhil/Documents/Painter/src/hallo.png') self.ui.rectange.clicked.connect(self.drawRect) self.pixmapItem = QGraphicsPixmapItem(self.pix) self.scene.addItem() def drawRect(self): self.pix.drawRect(0,0,40,40) print("function called") app = QApplication([]) window = Painter() window.show() app.exec_()When the rectangle button is clicked, it draws a rect on the pixmap. But, the QGraphicsScene doesn't update. I tried update() , but it didn't work. What did work was readding the pixmap. But, I think it's inconvenient. Is there a proper way to solve this?
RE: QPixmap not updating in QGraphicsScene after using QPainter on it - deanhystad - Apr-15-2022 When you draw in your Surface you are changing the pixmap in pix, not adding graphical items to the scene. In future posts try to provide example code that DOES NOT use designer. You will get faster responses if other people can run you code. |