Results 1 to 3 of 3

Thread: How to display image on QPlainTextEdit

  1. #1
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Post How to display image on QPlainTextEdit

    Hi there, I am looking for a way to display image(emoticons) on QPlainTextEdit.
    here is what I have done so far; this class is called by a QWidget class

    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui
    3. from PyQt4 import QtCore
    4.  
    5. class TextView(QtGui.QPlainTextEdit):
    6. def __init__(self,Ui_MainWindow, parent=None):
    7. QtGui.QPlainTextEdit.__init__(self, parent)
    8. self.setReadOnly(True)
    9.  
    10. cursor = self.textCursor()
    11. cursor.movePosition(QtGui.QTextCursor.NextWord, QtGui.QTextCursor.KeepAnchor)
    12. cursor.insertText("test")
    13.  
    14. self.setTextCursor(cursor)
    15.  
    16. imageCursor = self.textCursor()
    17. imageCursor.movePosition(QtGui.QTextCursor.NextWord, QtGui.QTextCursor.KeepAnchor)
    18.  
    19. icon = QtGui.QPixmap("image/emoticon.png")
    20. image = icon.toImage()
    21. if image.isNull():
    22. print 'null'
    23. imageCursor.insertImage(image)
    24. self.setTextCursor(imageCursor)
    To copy to clipboard, switch view to plain text mode 

    now it only displays "text" but not the image. What am I doing wrong? is there any ways to display image or icon onto QPlainTextEdit?

    I would appreciate advice if anyone has encountered this or a similar issue.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to display image on QPlainTextEdit

    You can't do that... that's why it's called plaintext edit. Use QTextEdit or QTextBrowser instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to display image on QPlainTextEdit

    thank you for your reply, wysota. yes that works!!
    here is my code for anyone who had a similar problem.

    Qt Code:
    1. class TextView(QtGui.QTextEdit):
    2. def __init__(self,Ui_MainWindow, parent=None):
    3. QtGui.QTextEdit.__init__(self, parent)
    4. self.setReadOnly(True)
    5. self.setAcceptRichText(True)
    6. def insert(self, string,emoticonList):
    7. cursor = self.textCursor()
    8. if not emoticonList:
    9. cursor.insertText(string)
    10. else:
    11. ls = shlex.split(str(string))
    12. for word in ls:
    13. for emoticon in emoticonList[:]:
    14. if word == emoticon:
    15. path = "image/emoticons/" + word
    16. icon = QtGui.QPixmap(path)
    17. image = icon.toImage()
    18. cursor.insertImage(image)
    19. else:
    20. cursor.insertText(word + " ")
    21. self.setTextCursor(cursor)
    22. end = "<br>"
    23. fragment = QtGui.QTextDocumentFragment.fromHtml(end)
    24. self.textCursor().insertFragment(fragment)
    25. def insertReceiveMessage(self, msg):
    26. cursor = self.textCursor()
    27. cursor.insertText(msg)
    28. self.setTextCursor(cursor)
    29. end = "<br>"
    30. fragment = QtGui.QTextDocumentFragment.fromHtml(end)
    31. self.textCursor().insertFragment(fragment)
    To copy to clipboard, switch view to plain text mode 

    thank you so much, wysota!

Similar Threads

  1. Replies: 2
    Last Post: 29th September 2008, 00:08
  2. Replies: 7
    Last Post: 13th August 2008, 18:27
  3. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. Display and divide the image
    By kiransu123 in forum Qt Programming
    Replies: 19
    Last Post: 5th March 2007, 20:40

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.