Results 1 to 5 of 5

Thread: Crashing QGraphicsTextItem when updating HTML in PyQt

  1. #1
    Join Date
    Oct 2019
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Crashing QGraphicsTextItem when updating HTML in PyQt

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashing QGraphicsTextItem when updating HTML in PyQt

    The QGraphicsTextItem does not have a default QTextDocument; you have to create one and set it before you can use it. So your call to self.document() is likely returning a null instance, thus the crash. QGraphicsTextItem does not take ownership of the document, allowing the document to be shared with multiple views.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Oct 2019
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashing QGraphicsTextItem when updating HTML in PyQt

    Ahh, thank you, I see. It's strange that it didn't then crash when it started as there is the setHTML in the constructor. If there's no associated text document it seems that would have crashed it too.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashing QGraphicsTextItem when updating HTML in PyQt

    Probably QGraphicsTextItem is implemented with the optional ability to use a QTextDocument, and has a default means to simply use an HTML or simple text string if no document is set.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Oct 2019
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashing QGraphicsTextItem when updating HTML in PyQt

    Thank you for the help. I couldn't find why this was crashing, but I was able to convert it to a simpleTextItem which is sufficient for this particular purpose. I'll have to revisit later, though, because I'll need the more complex one later.

Similar Threads

  1. Extract data from HTML string in Python with PyQt WebView
    By PythonHelp in forum Qt Programming
    Replies: 2
    Last Post: 3rd March 2017, 16:15
  2. Replies: 5
    Last Post: 6th April 2015, 10:14
  3. Crashing without debug mode- No crashing with gdb debugger
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2011, 12:27
  4. Replies: 51
    Last Post: 26th November 2010, 14:24
  5. How to design one new browser(not for HTML) with PyQt
    By ubuntu in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2010, 04:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.