当一个QGraphicsTextItem被添加到场景中时,我想做的是在我的QGraphicsView中显示一个QLineEdit。行编辑仅用于在双击时设置QGraphicsItem的文本,当按下返回键时需要删除。你知道吗
我很难删除QLineEdit。我试着简单地删除它时,返回键被按下,但它仍然没有删除。下面是重现这种行为的代码:
class Text(QGraphicsTextItem):
def __init__(self, text, position=QPointF(0,0), parent=None, scene=None):
super().__init__(text, parent=parent, scene=scene)
self.parent = parent
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.height = self.document().size().height()
self.width = self.document().size().width()
self.text_center = QPointF(-self.width/2, -self.height/2)
if parent:
self.parent_center = self.parent.boundingRect().center()
self.scene = self.parent.scene
self.setPos(text_center)
else:
self.setFlag(QGraphicsItem.ItemIsMovable)
self.scene = scene
self.setPos(position - self.text_center)
def mouseDoubleClickEvent(self, mouseEvent):
self.Editing = True
self.nameEdit = NameEditor(self)
self.nameEditProxy = self.scene.addWidget(self.nameEdit)
self.nameEditProxy.setPos(self.mapToScene(QPointF(0, 0)))
class NameEditor(QLineEdit):
def __init__(self, textItem):
super().__init__(textItem.toPlainText())
self.setMaximumWidth(200)
self.setFixedWidth(200)
self.selectAll()
self.grabKeyboard()
self.textItem = textItem
def returnPressed(self):
self.textItem.setPlainText(self.text())
del self
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QGraphicsView()
scene = QGraphicsScene()
scene.setSceneRect(0, 0, 500, 500)
view.setScene(scene)
text = Text("Example", position=QPointF(250, 250), scene=scene)
view.show()
sys.exit(app.exec_())
在此尝试中,我将通过del self
方法中的returnPressed
删除子类QLineEdit。我还尝试过通过Text
类的mouseDoubleClick
方法中的del self.nameEditProxy
删除包含它的QGraphicsProxyWidget。你知道吗
我的问题是如何删除returnPressed上的QLineEdit?你知道吗
在Qt中,从屏幕上删除小部件的一个简单方法是隐藏它们,也就是说,如果您不担心的话;也许您只是想摆脱它,否则就使用它
或者
您可以通过调用
或者
然后把它放在你想放的地方。你知道吗
相关问题 更多 >
编程相关推荐