我在这里做错了什么?我希望“image1.jpg”显示在“image.jpg”上,在我点击的位置,但事实并非如此。这是我的代码(image1.jpg比image.jpg小10倍):
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()
编辑1 我已尝试self.local_grview.setUpdatesEnabled(True)
并在pixelSelect方法结束时更新了场景:self.local_grview.update()
,没有任何变化
答案 0 :(得分:0)
您的代码看起来正确并且对我有效。第二个较小的图像显示在第一个。
您是否尝试仅显示第二张图片?也许您的路径不正确会导致您的第二张图片无法显示。