Pergunta

What am I doing wrong here? I expect that "image1.jpg" is shown over "image.jpg" ,at position where I've clicked, but it does not. Here is my code (image1.jpg is 10 times smaller then image.jpg):

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

class DrawImage(QMainWindow): 
    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)
        self.setWindowTitle('Select Window')
        self.local_image = QImage('image.JPG')
        self.local_grview = QGraphicsView()
        self.setCentralWidget( self.local_grview )
        self.local_scene = QGraphicsScene()
        self.image_format = self.local_image.format()
        self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)
        self.pixMapItem.setZValue(10.0)
        self.local_grview.setScene( self.local_scene )
        self.pixMapItem.mousePressEvent = self.pixelSelect

    def pixelSelect( self, event ):
        position = QPoint( event.pos().x(),  event.pos().y())
        local_image = QImage('image1.JPG')
        pixMapItem = QGraphicsPixmapItem(QPixmap(local_image), self.pixMapItem, self.local_scene)
        pixMapItem.setZValue(100.0)
        pixMapItem.setPos(position.x(), position.y());
        print position, self.pixMapItem.zValue(), pixMapItem.zValue()

def main():
    app = QtGui.QApplication(sys.argv)
    form = DrawImage()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Edit 1 I've tried self.local_grview.setUpdatesEnabled(True) and updating scene at the end of pixelSelect method: self.local_grview.update() , nothing changed

Foi útil?

Solução

Your code looks correct and works as expected for me ie. the second smaller image displays over the first.

Have you tried displaying just the second image? Perhaps you have an incorrect path which is causing your second image not to show.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top