Hello!

I'm working in PyQt and I have created a QGraphicsTextItem that I need to update (the text) from time to time. I need to center the text, so I read that I have to use the TextOptions class and then apply that to the QGraphicsTextItem.document(). Kind of confusing to me why the document is necessary, but that's what I've done.

The problem is when I update the text with QGraphicsTextItem.document().setHTML function it crashes. It gives nothing but an error return value and no other indications why it's crashing. I update the html in on_press() when any key is pressed. I've tried several approaches in setTextString in the DisplayKeyText class, but all of them end up crashing.

Any ideas how to fix this? Thanks!

Qt Code:
  1. import sys
  2. from PyQt5 import QtGui, QtWidgets
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtGui import QPainter, QColor
  5. from pynput import keyboard
  6.  
  7. class Geo:
  8. def __init__(self, x, y, w, h):
  9. self.x = x
  10. self.y = y
  11. self.w = w
  12. self.h = h
  13.  
  14. class DisplayKeyText(QtWidgets.QGraphicsTextItem):
  15. def __init__(self, txt, font_size=60, color="white", parent=None):
  16. super(DisplayKeyText, self).__init__(parent)
  17.  
  18. self.display = txt
  19. self.color = color
  20. self.font_size = font_size
  21.  
  22. font = QtGui.QFont("Helvetica",font_size) #, QtGui.QFont.Bold)
  23. #self.setHtml("<font color='{}' >{}</font>".format(color, self.display))
  24. self.setFont(font)
  25.  
  26. txt_width = 100
  27. self.setTextWidth(txt_width)
  28. self.document().setTextWidth(txt_width)
  29. options = QtGui.QTextOption(Qt.AlignHCenter)
  30. self.document().setDefaultTextOption(options)
  31. self.document().setHtml("<font color='{}' >{}</font>".format(color, self.display))
  32.  
  33. def setTextProps(self, color, font_size, txt=None):
  34. self.setHtml("<font color='{}' size={}>{}</font>"
  35. .format(color, font_size, self.display if txt is None else txt))
  36.  
  37. def setTextString(self, txt):
  38. self.display = txt
  39. html_txt = "<font color='{}' size={}>{}</font>".format(self.color, self.font_size, txt)
  40. #self.document().setHtml("hello")
  41. self.document().setHtml("<div>hello</div>")
  42. print("x = {}, y = {}, width = {}".format(self.x(), self.y(), self.document().textWidth()))
  43.  
  44.  
  45. class MainWindow(QtWidgets.QMainWindow):
  46. def __init__(self, parent=None):
  47. super(MainWindow, self).__init__(parent)
  48.  
  49. self.scene = QtWidgets.QGraphicsScene(self)
  50. self.scene.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex)
  51. self.txt = DisplayKeyText("X", 20)
  52. self.scene.addItem(self.txt)
  53.  
  54. self.create_central_widget()
  55. self.listener = keyboard.Listener(
  56. on_press=self.on_press,
  57. on_release=self.on_release)
  58. self.listener.start()
  59.  
  60. def create_central_widget(self):
  61. self.view = QtWidgets.QGraphicsView(self.scene)
  62. self.view.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)
  63. self.view.setBackgroundBrush((QColor("bisque")))
  64. self.setCentralWidget(self.view)
  65. self.view.scale(3, 3)
  66.  
  67. def on_press(self, key):
  68. self.txt.setTextString("*")
  69.  
  70. def on_release(self, key):
  71. pass
  72.  
  73.  
  74. def main():
  75. app = QtWidgets.QApplication(sys.argv)
  76. w = MainWindow()
  77. w.show()
  78. ret = app.exec_()
  79. sys.exit(ret)
  80.  
  81. if __name__ == "__main__":
  82. main()
To copy to clipboard, switch view to plain text mode