【问题标题】:Edit QGraphicsTextItem inside QGraphicsItemGroup在 QGraphicsItemGroup 中编辑 QGraphicsTextItem
【发布时间】:2013-03-12 18:34:10
【问题描述】:

我想要一个 python 应用程序,它显示一堆小肖像和它下面的名字。像那样:

它们应该是可移动和可编辑的(通过双击文本)。

我正在使用 PyQt4,所以我发现,最简单的方法是使用 QGraphicsViewQGraphicsScene 作为画布。所以我像这样子类化了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 的文本交互模式可能就足够了。谢谢!

【问题讨论】:

    标签: python qt pyqt4 qgraphicsview qgraphicstextitem


    【解决方案1】:

    也许你应该调用 QGraphicsItemGroup::setHandlesChildEvents(false)。

    查看 SO 问题Events with QGraphicsItemGroup

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多