我想從QGraphicsView中獲取各種鼠標事件的座標,但我無法弄清楚如何觸發它們。最後,我想添加一個圖片到graphicsView,然後在其上繪製。用於QGraphics的PyQt鼠標事件查看
理想我想的座標在頂部的原點左
0,0--------------------
|
|
|
|
|
|
|
test.py
import sys
from PyQt4 import QtCore, QtGui, uic
class test(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi('test.ui', self)
self.connect(self.ui.graphicsView, QtCore.SIGNAL("mousePressEvent()"), self.mouse_pressed)
self.connect(self.ui.graphicsView, QtCore.SIGNAL("mouseMoveEvent()"), self.mouse_moved)
self.connect(self.ui.graphicsView, QtCore.SIGNAL("mouseReleaseEvent()"), self.mouse_released)
self.ui.show()
def mouse_pressed(self):
p = QtGui.QCursor.pos()
print "pressed here: " + p.x() + ", " + p.y()
def mouse_moved(self):
p = QtGui.QCursor.pos()
print "moved here: " + p.x() + ", " + p.y()
def mouse_released(self):
p = QtGui.QCursor.pos()
print "released here: " + p.x() + ", " + p.y()
def main():
app = QtGui.QApplication(sys.argv)
ui = test()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
test.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGraphicsView" name="graphicsView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
編輯:
這似乎是上班。由於點擊事件似乎接管,橢圓不再可移動。有任何想法嗎?
import sys
from PyQt4 import QtCore, QtGui, uic
class graphicsScene(QtGui.QGraphicsScene):
def __init__ (self, parent=None):
super(graphicsScene, self).__init__ (parent)
def mousePressEvent(self, event):
position = QtCore.QPointF(event.scenePos())
print "pressed here: " + str(position.x()) + ", " + str(position.y())
self.update()
def mouseMoveEvent(self, event):
position = QtCore.QPointF(event.scenePos())
print "moved here: " + str(position.x()) + ", " + str(position.y())
self.update()
def mouseReleaseEvent(self, event):
position = QtCore.QPointF(event.scenePos())
print "released here: " + str(position.x()) + ", " + str(position.y())
self.update()
class test(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi('test.ui', self)
self.scene = graphicsScene()
self.ui.graphicsView.setScene(self.scene)
pen = QtGui.QPen(QtCore.Qt.red)
brush = QtGui.QBrush(QtCore.Qt.blue)
e = self.scene.addEllipse(10,10,100,100, pen, brush)
e.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
self.ui.show()
def main():
app = QtGui.QApplication(sys.argv)
ui = test()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我認爲您的意思是:「您*無法*在這些方法上使用'connect'」。而且,只需重寫'mousePressEvent()'比安裝事件過濾器更簡單,更簡潔。 – jmk
這將需要OP子類'QGraphicsView'並促進在UI文件中的這個子類的視圖。這是一個過度複雜的過程。如果想要改變視圖的行爲並在幾個不同的地方重用該類,那麼'QGraphicsView'的子類化將是明智的。我認爲這在簡單情況下不是必需的。 –
我想我照你的意思去做了。任何想法爲什麼着色我的橢圓不起作用? – waspinator