我想要一个 python 应用程序,它显示一堆小肖像和它下面的名字。像那样:
它们应该是可移动和可编辑的(通过双击文本)。
我正在使用 PyQt4,所以我发现,在画布上使用 aQGraphicsView
和 a是最简单的。QGraphicsScene
所以我将一个这样的子类化QGraphicsItemGroup
:
from PyQt4 import QtCore, QtGui
class Speaker(QtGui.QGraphicsItemGroup):
def __init__(self, name, parent=None):
QtGui.QGraphicsItemGroup.__init__(self, parent)
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
self.text = QtGui.QGraphicsTextItem(name)
self.text.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction)
self.addToGroup(self.text)
self.portrait = QtGui.QGraphicsPixmapItem(QtGui.QPixmap("portrait.png"))
self.portrait.setY(-35)
self.addToGroup(self.portrait)
def keyPressEvent(self, QKeyEvent):
# Forwarding KeyPress events to the text to enable text editing
self.text.keyPressEvent(QKeyEvent)
但是有一些问题:
- 文本编辑由单击触发,但我想要双击(可能与this重复)。
- 您不能使用鼠标选择文本或移动光标,因为整个组都会被移动。
- 如果停止编辑,光标不会消失。(虽然我知道该怎么做,但如果我找到激活和停用编辑模式的方法)
我试图捕捉双击信号并切换到将所有鼠标事件转发到文本的编辑模式。但是我无法通过双击激活编辑过程,而且我无法保留通过单击其他位置来结束编辑的行为。
所以我希望有人能帮助我。知道如何手动激活和停用QGraphicsTextItem
. 谢谢!