如何重新实现QGraphicsPixmapItem的itemChange和mouseMoveEvent?

问题描述 投票:1回答:1

我正在使用PySide建立一个T恤设计师。为此,我在场景中设置了一个QGraphicsScene,其中T Shirt的图像作为QPixmapItem。为了在T恤上叠加设计,我从用户那里获得设计图像PNG并将其设置为另一个QPixmapItem。我使用setPos()方法对齐它们,然后使用setZValue()方法确保设计PNG显示在T恤图像的顶部。

我为设计图像QPixmapItem启用了标志ItemIsMovable,ItemIsSelectable,ItemSendsScenePositionChanges,ItemIsFocusable。所以我能够在T恤图像上移动设计。接下来,我想将此移动仅限于可以打印的位置。为了实现这一点,我遵循this question派生了一个新的QGraphicsPixmapItem类,并尝试重新实现itemChange()和mouseMoveEvent()方法。

在这些方法中,我尝试使用super()以及常规方式QGraphicsPixmapItem.itemChange(change,event)调用原始QPixmapItem类的相同方法。然而,似乎没有任何事情发生。设计移动得很好,但不会受到限制。要查看方法是否被调用,我在这些方法中添加了print语句,但它们没有被执行。

我也尝试在场景中添加setSceneRect()。我还在QGraphicsView上启用了setMouseTracking。但是,这些都不会触发itemChanged()或mouseMoveEvent()。

还有其他问题,人们已经解释了如何在C ++中执行此操作。但是,我无法在python中复制它。

# -*- coding: utf-8 -*-

from PySide import QtCore, QtGui
from os import path

class Pixmap(QtGui.QGraphicsPixmapItem):

    def __init__(self, pix):
        super(Pixmap, self).__init__()
        self.pixmap_item = QtGui.QGraphicsPixmapItem(pix)
        self.pixmap_item.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        self.pixmap_item.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
        self.pixmap_item.setFlag(QtGui.QGraphicsItem.ItemSendsScenePositionChanges, True)
        self.pixmap_item.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)

    def itemChange(self, change, event):
        QtGui.QGraphicsPixmapItem.itemChange(change, event)
        print "Item Changed"
        #Code to restrict to a rectangular area goes here
        return QtGui.QGraphicsPixmapItem.itemChange(self, change, event)

    def mouseMoveEvent(self, event):
        super(Pixmap, self).mouseMoveEvent(event)
        print "Mouse Moved"
        #Code to restrict to a rectangular area goes here

class Ui_frmSelectRoundNeckHalfSleeve(QtCore.QObject):

    def setupUi(self, frmSelectRoundNeckHalfSleeve):
        frmSelectRoundNeckHalfSleeve.setObjectName("frmSelectRoundNeckHalfSleeve")
        frmSelectRoundNeckHalfSleeve.resize(842, 595)
        self.imgRoundNeckTShirt = QtGui.QGraphicsView(frmSelectRoundNeckHalfSleeve)
        self.imgRoundNeckTShirt.setGeometry(QtCore.QRect(20, 20, 500, 500))
        self.imgRoundNeckTShirt.setObjectName("imgRoundNeckTShirt")
        self.imgRoundNeckTShirt.setMouseTracking(True)

        self.tShirtScene = QtGui.QGraphicsScene(frmSelectRoundNeckHalfSleeve)
        self.tShirtScene.setSceneRect(20, 20, 480, 480)
        self.TShirtImage = QtGui.QGraphicsPixmapItem(QtGui.QPixmap("./Images/black-t-shirt.jpg").scaled(480, 480, QtCore.Qt.KeepAspectRatio))
        self.designImagePixmap = QtGui.QPixmap("./Designs/test.png")
        self.designImagePng = Pixmap(self.designImagePixmap.scaledToWidth(135,QtCore.Qt.SmoothTransformation))
        self.TShirtImage.setZValue(10)
        self.designImagePng.pixmap_item.setZValue(40)
        self.designImagePng.pixmap_item.setPos(167,90)
        self.tShirtScene.addItem(self.TShirtImage)
        self.tShirtScene.addItem(self.designImagePng.pixmap_item)
        self.imgRoundNeckTShirt.setScene(self.tShirtScene)

if __name__ == "__main__":

    path = r"E:\\Documents\\T Shirt Designer\\"
    QtGui.QApplication.addLibraryPath(path)

    app = QtGui.QApplication(sys.argv)
    testFile = QtGui.QWidget()
    ui = Ui_frmSelectRoundNeckHalfSleeve()
    ui.setupUi(testFile)

    testFile.show()
    sys.exit(app.exec_())
python python-2.7 pyside qgraphicsitem qgraphicspixmapitem
1个回答
0
投票

您遇到的错误是您没有覆盖pixmap_item itemChange而是覆盖Pixmap。看来你的继承与构图混淆了。

一个改进是基础项目(T恤)是设计项目的父亲,以便设计项目的坐标相对于基础项目。

考虑到之前的事情,我已经实现了限制设计项目移动到T恤项目空间的逻辑。

# -*- coding: utf-8 -*-

import sys
from os import path
from PySide import QtCore, QtGui


class Pixmap(QtGui.QGraphicsPixmapItem):
    def __init__(self, pix, parent=None):
        super(Pixmap, self).__init__(pix, parent)
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QtGui.QGraphicsItem.ItemSendsScenePositionChanges, True)
        self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)

    def itemChange(self, change, value):
        if change == QtGui.QGraphicsItem.ItemPositionChange:
            parent = self.parentItem()
            if parent is not None:
                r = self.mapToParent(self.boundingRect()).boundingRect()
                R = parent.boundingRect()
                rR = QtCore.QRectF(R.topLeft(), R.size() - r.size())
                if not rR.contains(value):
                    x = min(max(rR.left(), value.x()), rR.right())
                    y = min(max(rR.top(), value.y()), rR.bottom())
                    return QtCore.QPointF(x, y)
        return QtGui.QGraphicsPixmapItem.itemChange(self, change, value)


class Ui_frmSelectRoundNeckHalfSleeve(QtCore.QObject):
    def setupUi(self, frmSelectRoundNeckHalfSleeve):
        frmSelectRoundNeckHalfSleeve.setObjectName(
            "frmSelectRoundNeckHalfSleeve"
        )
        frmSelectRoundNeckHalfSleeve.resize(842, 595)
        self.imgRoundNeckTShirt = QtGui.QGraphicsView(
            frmSelectRoundNeckHalfSleeve
        )
        self.imgRoundNeckTShirt.setGeometry(QtCore.QRect(20, 20, 500, 500))
        self.imgRoundNeckTShirt.setObjectName("imgRoundNeckTShirt")

        self.tShirtScene = QtGui.QGraphicsScene(frmSelectRoundNeckHalfSleeve)
        self.tShirtScene.setSceneRect(20, 20, 480, 480)
        self.TShirtImage = QtGui.QGraphicsPixmapItem(
            QtGui.QPixmap("./Images/black-t-shirt.jpg").scaled(
                480, 480, QtCore.Qt.KeepAspectRatio
            )
        )
        designImagePixmap = QtGui.QPixmap("./Designs/test.png").scaledToWidth(
            135, QtCore.Qt.SmoothTransformation
        )
        self.designImagePng = Pixmap(designImagePixmap, self.TShirtImage)
        self.designImagePng.setZValue(1)
        self.designImagePng.setPos(167, 90)
        self.tShirtScene.addItem(self.TShirtImage)
        self.imgRoundNeckTShirt.setScene(self.tShirtScene)


if __name__ == "__main__":

    path = r"E:\\Documents\\T Shirt Designer\\"
    QtGui.QApplication.addLibraryPath(path)
    app = QtGui.QApplication(sys.argv)
    testFile = QtGui.QWidget()
    ui = Ui_frmSelectRoundNeckHalfSleeve()
    ui.setupUi(testFile)
    testFile.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.