Python Forum
QPixmap not updating in QGraphicsScene after using QPainter on it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QPixmap not updating in QGraphicsScene after using QPainter on it
#1
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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Qpainter Error GMCobraz 6 4,414 Aug-26-2020, 06:57 AM
Last Post: GMCobraz
  [PyQt] QPainter issue showing black screen mart79 0 2,011 May-06-2020, 12:02 PM
Last Post: mart79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020