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
Code:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class TextView(QtGui.QPlainTextEdit):
def __init__(self,Ui_MainWindow, parent=None):
QtGui.QPlainTextEdit.__init__(self, parent)
self.setReadOnly(True)
cursor = self.textCursor()
cursor.insertText("test")
self.setTextCursor(cursor)
imageCursor = self.textCursor()
icon
= QtGui.
QPixmap("image/emoticon.png") image = icon.toImage()
if image.isNull():
print 'null'
imageCursor.insertImage(image)
self.setTextCursor(imageCursor)
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.
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.
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.
Code:
def __init__(self,Ui_MainWindow, parent=None):
self.setReadOnly(True)
self.setAcceptRichText(True)
def insert(self, string,emoticonList):
cursor = self.textCursor()
if not emoticonList:
cursor.insertText(string)
else:
ls = shlex.split(str(string))
for word in ls:
for emoticon in emoticonList[:]:
if word == emoticon:
path = "image/emoticons/" + word
image = icon.toImage()
cursor.insertImage(image)
else:
cursor.insertText(word + " ")
self.setTextCursor(cursor)
end = "<br>"
self.textCursor().insertFragment(fragment)
def insertReceiveMessage(self, msg):
cursor = self.textCursor()
cursor.insertText(msg)
self.setTextCursor(cursor)
end = "<br>"
self.textCursor().insertFragment(fragment)
thank you so much, wysota!