Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyqt获取图像位置_Python_Pyqt_Qgraphicsview_Qgraphicsscene - Fatal编程技术网

Python pyqt获取图像位置

Python pyqt获取图像位置,python,pyqt,qgraphicsview,qgraphicsscene,Python,Pyqt,Qgraphicsview,Qgraphicsscene,我有一个QGraphicscene,上面有一个进入QGraphicsView的图片QPixmap。我决定使用QPixmap QGraphicsItem.ItemIsMovable。我想知道图片移动时的位置。我该怎么做?如果您只想知道当前图像位置,可以从pixmapItem.pos()或pixmapItem.x()和pixmapItem.y()接收 但是如果您需要知道,当它被移动时,您可以子类化QGraphicsPixmapItem,设置QGraphicsItem.itemssendsgeomet

我有一个QGraphicscene,上面有一个进入QGraphicsView的图片QPixmap。我决定使用QPixmap QGraphicsItem.ItemIsMovable。我想知道图片移动时的位置。我该怎么做?

如果您只想知道当前图像位置,可以从
pixmapItem.pos()
pixmapItem.x()
pixmapItem.y()
接收

但是如果您需要知道,当它被移动时,您可以子类化
QGraphicsPixmapItem
,设置
QGraphicsItem.itemssendsgeometrychanges
标志,并在覆盖的
itemChange
函数中接收通知:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui


class MovablePixmapItem(QtGui.QGraphicsPixmapItem):
    def __init__(self, pixmap, *args, **kwargs):
        QtGui.QGraphicsPixmapItem.__init__(self, pixmap, *args, **kwargs)
        self.setFlags(QtGui.QGraphicsItem.ItemIsMovable |
                      QtGui.QGraphicsItem.ItemSendsGeometryChanges)

    def itemChange(self, change, value):
        if change == QtGui.QGraphicsItem.ItemPositionHasChanged:
            print value.toPointF()
            # do other work, or emit signal
        return QtGui.QGraphicsPixmapItem.itemChange(self, change, value)


class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.pixmapItem = MovablePixmapItem(QtGui.QPixmap('image.png'))
        self.scene = QtGui.QGraphicsScene()
        self.scene.addItem(self.pixmapItem)
        self.view = QtGui.QGraphicsView(self.scene)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.view)
        self.setLayout(layout)

    def do_test(self):
        pass


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = MainWidget()
    widget.resize(640, 480)
    widget.show()
    sys.exit(app.exec_())

如果您只想知道当前图像位置,可以从
pixmapItem.pos()
pixmapItem.x()
pixmapItem.y()
接收

但是如果您需要知道,当它被移动时,您可以子类化
QGraphicsPixmapItem
,设置
QGraphicsItem.itemssendsgeometrychanges
标志,并在覆盖的
itemChange
函数中接收通知:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui


class MovablePixmapItem(QtGui.QGraphicsPixmapItem):
    def __init__(self, pixmap, *args, **kwargs):
        QtGui.QGraphicsPixmapItem.__init__(self, pixmap, *args, **kwargs)
        self.setFlags(QtGui.QGraphicsItem.ItemIsMovable |
                      QtGui.QGraphicsItem.ItemSendsGeometryChanges)

    def itemChange(self, change, value):
        if change == QtGui.QGraphicsItem.ItemPositionHasChanged:
            print value.toPointF()
            # do other work, or emit signal
        return QtGui.QGraphicsPixmapItem.itemChange(self, change, value)


class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.pixmapItem = MovablePixmapItem(QtGui.QPixmap('image.png'))
        self.scene = QtGui.QGraphicsScene()
        self.scene.addItem(self.pixmapItem)
        self.view = QtGui.QGraphicsView(self.scene)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.view)
        self.setLayout(layout)

    def do_test(self):
        pass


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = MainWidget()
    widget.resize(640, 480)
    widget.show()
    sys.exit(app.exec_())