背景:我正在尝试使用Qt Graphics View框架在python中制作VR演示,该框架允许自定义呈现的GUI。我可以选择包装Qt的两个不同的python模块:PyQt5
和PySide2
。不幸的是,每个模块似乎都在Graphics View Framework中缺少了不同的关键要素。
PyQt5
似乎缺少QGraphicsSceneMouseEvent
的构造函数,而从VR控制器手势创建合成鼠标事件则需要该构造函数。这个简短的PyQt5程序...
from PyQt5.QtCore import QEvent, QPointF, Qt
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QGraphicsSceneMouseEvent
from PyQt5.QtGui import QOpenGLPaintDevice # No problem for PyQt5
pos = QPointF(20, 20)
event1 = QMouseEvent(QEvent.MouseMove, pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
device = QOpenGLPaintDevice(100, 100)
# Problem: TypeError: PyQt5.QtWidgets.QGraphicsSceneMouseEvent cannot be instantiated or sub-classed
event2 = QGraphicsSceneMouseEvent(QEvent.GraphicsSceneMouseMove)
...结果为TypeError: PyQt5.QtWidgets.QGraphicsSceneMouseEvent cannot be instantiated or sub-classed
看似好消息是,替代Qt绑定PySide2
模块可以构造一个QGraphicsSceneMouseEvent
而不会引起抱怨。但是PySide2
缺少QOpenGLPaintDevice类,我实际上需要绘制它。这个非常相似的程序,使用PySide2 ...
from PySide2.QtCore import QEvent, QPointF, Qt
from PySide2.QtGui import QMouseEvent
from PySide2.QtWidgets import QGraphicsSceneMouseEvent
pos = QPointF(20, 20)
event1 = QMouseEvent(QEvent.MouseMove, pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
event2 = QGraphicsSceneMouseEvent(QEvent.GraphicsSceneMouseMove) # No Problem for PySide2
from PySide2.QtGui import QOpenGLPaintDevice # Problem: PySide2 does not have this class
device = QOpenGLPaintDevice(100, 100)
得出ImportError: cannot import name 'QOpenGLPaintDevice'
我不确定有人能成功使用python的Qt Graphics View框架。如果没有,我想成为第一个。
答案 0 :(得分:0)
在我看来,并不是PyQt5中未启用QGraphicsSceneMouseEvent的错误,而是PySide2中已启用的错误,因为如果您查看the docs,就会发现QGraphicsSceneMouseEvent没有公共构造函数
如果要在Qt Graphics Framework中模拟鼠标移动,则必须将QMouseEvent发送到QGraphicsView的viewport()
。
在下面的示例中,我演示如何通过使用QMouseEvent模拟鼠标来移动项目:
from functools import partial
from PySide2 import QtCore, QtGui, QtWidgets
# from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.m_scene = QtWidgets.QGraphicsScene(QtCore.QRectF(0, 0, 400, 400), self)
self.m_graphicsview = QtWidgets.QGraphicsView(self.m_scene)
self.setCentralWidget(self.m_graphicsview)
self.resize(640, 480)
self.m_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(-50, -50, 100, 100))
self.m_item.setFlags(
self.m_item.flags() | QtWidgets.QGraphicsItem.ItemIsMovable
)
self.m_item.setBrush(QtGui.QColor("salmon"))
self.m_item.setPos(100, 100)
self.m_scene.addItem(self.m_item)
QtCore.QTimer.singleShot(1000, self.emulate_move_item)
def emulate_move_item(self):
sp = self.m_item.mapToScene(self.m_item.boundingRect().center())
lp = self.m_graphicsview.mapFromScene(sp)
end_pos = lp + QtCore.QPoint(100, 100)
self.press(lp)
animation = QtCore.QVariantAnimation(
self,
startValue=lp,
endValue=end_pos
)
animation.valueChanged.connect(self.moveTo)
animation.finished.connect(partial(self.release, end_pos))
animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
def press(self, pos):
event = QtGui.QMouseEvent(
QtCore.QEvent.MouseButtonPress,
pos,
self.m_graphicsview.mapToGlobal(pos),
QtCore.Qt.LeftButton,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier,
)
QtCore.QCoreApplication.postEvent(self.m_graphicsview.viewport(), event)
def moveTo(self, pos):
event = QtGui.QMouseEvent(
QtCore.QEvent.MouseMove,
pos,
self.m_graphicsview.viewport().mapToGlobal(pos),
QtCore.Qt.LeftButton,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier,
)
QtCore.QCoreApplication.postEvent(self.m_graphicsview.viewport(), event)
def release(self, pos):
event = QtGui.QMouseEvent(
QtCore.QEvent.MouseButtonRelease,
pos,
self.m_graphicsview.viewport().mapToGlobal(pos),
QtCore.Qt.LeftButton,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier,
)
QtCore.QCoreApplication.postEvent(self.m_graphicsview.viewport(), event)
def double_click(self, pos):
event = QtGui.QMouseEvent(
QtCore.QEvent.MouseButtonDblClick,
pos,
self.m_graphicsview.viewport().mapToGlobal(pos),
QtCore.Qt.LeftButton,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier,
)
QtCore.QCoreApplication.postEvent(self.m_graphicsview.viewport(), event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
结论:
不能在python中启用QGraphicsSceneMouseEvent构造函数,因为在C ++中Qt的公共API中无法访问它。因此PyQt5的行为是正确的,但是PySide2有一个错误。
PySide2没有实现QOpenGLPaintDevice类是一个可能的错误。