嗨,这是我先前的查询中的一个问题--从QGraphicsItems的上下文菜单操作中获取事件--我现在正在尝试从myQgraphicsItem发出一个信号,目标是当用户右击并选择action1时,notifyaction1函数将告诉场景做一些事情(向发送信号的图形站点添加更多的项目),我发现QgraphicsItem无法发出,请参阅*Stack溢出:‘s QGraphicsItem中的事件和信号:这个是如何工作的?
因此,我在类中添加了一个signaling_object (QObject),但我不确定如何发送/接收信号,以便父场景能够得到用户操作的通知。
class Node(QtGui.QGraphicsItem):
Type = QtGui.QGraphicsItem.UserType + 1
def __init__(self, Parent=None):
super(Node, self).__init__()
self.edgeList = []
self.newPos = QtCore.QPointF()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable)
self.setFlag(QtGui.QGraphicsItem.ItemSendsGeometryChanges)
self.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
self.setZValue(1)
self.signaling_object=QtCore.QObject()
@QtCore.pyqtSlot()
def notifyaction(self):
#print "action1"
message="action1"
QtCore.QObject.emit(self.signaling_object,QtCore.SIGNAL('action_update(QString)'),str(message))
print self.signaling_object.signalsBlocked()
child_items=self.childItems()
for item in child_items:
#print item
item.hide()
self.hide()
def contextMenuEvent(self, contextEvent):
object_cntext_Menu = QtGui.QMenu()
object_cntext_Menu.addAction("action1")
object_cntext_Menu.addAction("action2", self.notifyaction)
object_cntext_Menu.addAction("action3")
position=QtGui.QCursor.pos()
object_cntext_Menu.exec_(position)是否可以从qgraphicsItem的上下文菜单向其他qt对象发送通知。我正在windows上使用PyQt/Python,非常感谢
发布于 2014-04-10 20:42:42
如果您想要图形项的信号/插槽支持,可以使用QGraphicsObject而不是QGraphicsItem。这样你就可以发出海关信号,像这样:
class Node(QtGui.QGraphicsObject):
customSignal = QtCore.pyqtSignal(str)
...
@QtCore.pyqtSlot()
def notifyaction(self):
message = 'action1'
self.customSignal.emit(message)要接收这些自定义信号,只需连接适当的处理程序:
item.customSignal.connect(scene.handleCustomSignal)然而,它可能更简单,完全避免信号,只是直接打电话现场。每个图形项目都可以通过其景物方法访问其添加到的场景。所以你可以做这样简单的事情:
@QtCore.pyqtSlot()
def notifyaction(self):
message = 'action1'
scene = self.scene()
if scene is not None:
scene.handleItemAction(self, message)这样做意味着您不必为创建的每个图形项连接信号。
https://stackoverflow.com/questions/22997321
复制相似问题