python - Removing QLineEdit proxy widget from QGraphicsScene on returnPressed -
what have qlineedit appear in qgraphicsview when qgraphicstextitem added scene. line edit being used set text of qgraphicsitem when double-clicked , needs removed when return key pressed.
i'm having trouble getting qlineedit deleted. i've tried deleting when return key pressed still not removed. here code reproduce behaviour:
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_())
i deleting subclassed qlineedit via del self
in returnpressed
method in attempt. i've tried deleting qgraphicsproxywidget contains via del self.nameeditproxy
in mousedoubleclick
method of text
class.
my question how can delete qlineedit on returnpressed?
in qt, simple method of removing widgets screen hide them, if not worried that; perhaps want rid of it, otherwise use
mywidget.setvisible(false)
or
mywidget.hide();
you can reuse widget calling
mywidget.setvisible(true)
or
mywidget.show()
and repositioning anywhere want.
Comments
Post a Comment