QGraphicsPixmapItem 不会显示在其他 QGraphicsPixmapItem 之上 [英] QGraphicsPixmapItem won't show over the other QGraphicsPixmapItem

查看:47
本文介绍了QGraphicsPixmapItem 不会显示在其他 QGraphicsPixmapItem 之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么?我希望image1.jpg"显示在image.jpg"上,在我点击的位置,但事实并非如此.这是我的代码(image1.jpg 比 image.jpg 小 10 倍):

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 我试过 self.local_grview.setUpdatesEnabled(True) 并在 pixelSelect 方法的末尾更新场景:self.local_grview.update() ,什么都没有改变

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

推荐答案

您的代码看起来正确并且对我来说按预期工作,即.第二个较小的图像显示在第一个之上.

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.

这篇关于QGraphicsPixmapItem 不会显示在其他 QGraphicsPixmapItem 之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆