2024-06-28 20:33:21 发布
网友
我有一个qgraphicscene,上面有一个图片QPixmap到QGraphicsView中。我决定去QPixmapQGraphicsItem.items可移动. 我想知道照片移动时的位置。我该怎么做?在
如果您只想知道当前图像的位置,可以从pixmapItem.pos()或pixmapItem.x()和{}接收它。在
pixmapItem.pos()
pixmapItem.x()
但是,如果您需要知道,当它被移动时,您可以子类QGraphicsPixmapItem,设置QGraphicsItem.ItemSendsGeometryChanges标志,并在重写的{}函数中接收通知:
QGraphicsPixmapItem
QGraphicsItem.ItemSendsGeometryChanges
#!/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()
和{但是,如果您需要知道,当它被移动时,您可以子类}函数中接收通知:
QGraphicsPixmapItem
,设置QGraphicsItem.ItemSendsGeometryChanges
标志,并在重写的{相关问题 更多 >
编程相关推荐