Apr-11-2022, 12:26 PM
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?