我遇到一种情况,我想在QGraphicsScene
的{{1}}中显示的图像上绘制圆圈(见屏幕截图)。
我目前正在通过覆盖自定义QGraphicsView
对象的mousePressEvent
放置红色圆圈,但是这似乎防止了鼠标事件传播到形成QGraphicsScene
的对象。界。真可惜,因为我希望它们保持可移动性和选择性。我目前的技巧是维护生成的QGraphicsEllipseItem
的列表,并简单地遍历它们,调用它们的事件处理程序,例如:
QGraphicsItem
这种工作方式有效,但似乎有点问题(有时椭圆变得无法选择)。我应该提到this post的解决方案对我不起作用。如果我尝试使用def mouseMoveEvent(self, event):
for circ in self.circs:
if circ.rect().contains(event.scenePos()) or circ.isSelected():
circ.mouseMoveEvent(event)
之类的方式传播事件,则会收到错误消息:
QtGui.QGraphicsItem.mouseMoveEvent(self, event)
那么正确的正确方法是什么?下面是一个代码示例。
main.py:
Traceback (most recent call last):
File "main.py", line 58, in mouseMoveEvent
QtGui.QGraphicsItem.mouseMoveEvent(self, event)
AttributeError: module 'PySide2.QtGui' has no attribute 'QGraphicsItem'
main_window.py:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2 import QtWidgets, QtGui
import ui.main_window as main_window
import sys
import data_loader.data_loaders as module_data
import cv2 as cv
img_default_size = (512, 512)
class WingSceneWidget(QtWidgets.QGraphicsScene):
def __init__(self, parent):
super(self.__class__, self).__init__(parent)
self.keypoints = []
self.annotating = False
self.p_item = None
self.circs = []
def mouseDoubleClickEvent(self, event):
print("New Annotation")
self.annotating = True
self.keypoints = []
for circ in self.circs:
self.removeItem(circ)
def mousePressEvent(self, event):
circ_radius = 5
if self.annotating:
x = event.scenePos().x()
y = event.scenePos().y()
self.keypoints.append([x, y])
circ = QGraphicsEllipseItem(x-circ_radius, y-circ_radius, circ_radius*2, circ_radius*2, self.p_item)
circ.setPen(QPen(Qt.red, 2))
circ.setFlag(QGraphicsItem.ItemIsMovable, True)
circ.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.circs.append(circ)
print(self.keypoints)
if len(self.keypoints) >= 8:
self.annotating = False
for circ in self.circs:
if circ.rect().contains(event.scenePos()):
circ.mousePressEvent(event)
def mouseMoveEvent(self, event):
for circ in self.circs:
if circ.rect().contains(event.scenePos()) or circ.isSelected():
circ.mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
for circ in self.circs:
if circ.rect().contains(event.scenePos()) or circ.isSelected():
circ.mouseReleaseEvent(event)
def give_pitem(self, p_item):
self.p_item = p_item
class WingNet(QtWidgets.QMainWindow, main_window.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.folder_list = []
self.scene = WingSceneWidget(self.gv_wing_image)
self.gv_wing_image.setScene(self.scene)
self.gv_wing_image.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
self.btn_label_wings.clicked.connect(self.browse_folders)
self.listWidget.currentItemChanged.connect(self.selection_changed)
def browse_folders(self):
self.listWidget.clear() # In case there are any existing elements in the list
file_dialog = QtWidgets.QFileDialog()
file_dialog.setFileMode(QtWidgets.QFileDialog.DirectoryOnly)
file_dialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
file_view = file_dialog.findChild(QtWidgets.QListView, 'listView')
# to make it possible to select multiple directories:
if file_view:
file_view.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
f_tree_view = file_dialog.findChild(QtWidgets.QTreeView)
if f_tree_view:
f_tree_view.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
if file_dialog.exec():
self.folder_list = file_dialog.selectedFiles()
if self.folder_list:
image_paths = module_data.get_image_paths(self.folder_list)
for image_path in image_paths:
print(image_path)
self.listWidget.addItem(image_path)
self.btn_edit_tps.setEnabled(False)
self.btn_label_wings.setText("Start")
self.btn_label_wings.clicked.disconnect()
def selection_changed(self):
selected = self.listWidget.currentItem().text()
print(selected)
image = cv.imread(selected)
image = cv.resize(image, img_default_size)
height, width, channel = image.shape
bytes_per_line = 3 * width
q_img = QtGui.QImage(image.data, width, height, bytes_per_line, QtGui.QImage.Format_RGB888).rgbSwapped()
pixmap = QtGui.QPixmap.fromImage(q_img)
p_item = self.scene.addPixmap(pixmap)
self.scene.give_pitem(p_item)
def main():
app = QtWidgets.QApplication(sys.argv)
form = WingNet()
form.show()
app.exec_()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
由于要覆盖场景mousePressEvent,因此必须自己检查鼠标坐标处存在哪些项目;这可以通过使用QGraphicsScene.items()
来实现,它以z索引顺序(从上到下)返回这些坐标处的项目列表。
在您的情况下,只需确保存在某些项目,并且最顶层是QGraphicsEllipseItem;如果是这种情况,请继续进行基类的实现,否则请添加要点。这也意味着您不必重写mouseMoveEvent或mouseReleaseEvent。
def mousePressEvent(self, event):
items = self.items(event.scenePos())
if items and isinstance(items[0], QGraphicsEllipseItem):
QtWidgets.QGraphicsScene.mousePressEvent(self, event)
return
if self.annotating:
circ_radius = 5
x = event.scenePos().x()
y = event.scenePos().y()
self.keypoints.append([x, y])
circ = QGraphicsEllipseItem(x-circ_radius, y-circ_radius, circ_radius*2, circ_radius*2, self.p_item)
circ.setPen(QPen(Qt.red, 2))
circ.setFlags(circ.flags() | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
if len(self.keypoints) >= 8:
self.annotating = False
为了将来提供参考,请尝试提供尽可能少且可重现的示例:不需要整个加载机制,也不需要cv模块依赖性。