QTextEdit并将图像保存到.html

时间:2018-12-22 10:43:10

标签: python qt pyqt pyqt5

如何使用Q <image>从QTextEdit获取图像以将其保存在.html文件中?

通常的QTextEdit.ToHtml()不起作用。

1 个答案:

答案 0 :(得分:0)

因此,未定义标签pyqt的标签应为pyqt5(由于您未尝试发布代码而没有付出努力,因此我将忽略pyqt5标签。因此,您需要修改脚本您可以根据自己的喜好将其与pyqt5配合使用。

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class TextView(QtGui.QTextEdit):

    def __init__(self,Ui_MainWindow, parent=None):
        QtGui.QTextEdit.__init__(self, parent)
        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
                        icon = QtGui.QPixmap(path)
                        image = icon.toImage()
                        cursor.insertImage(image)
                    else:
                        cursor.insertText(word + " ")
        self.setTextCursor(cursor)
        end = "<br>"
        fragment = QtGui.QTextDocumentFragment.fromHtml(end)
        self.textCursor().insertFragment(fragment)

    def insertReceiveMessage(self, msg):
        cursor = self.textCursor()
        cursor.insertText(msg)
        self.setTextCursor(cursor)
        end = "<br>"
        fragment = QtGui.QTextDocumentFragment.fromHtml(end)
        self.textCursor().insertFragment(fragment)

可以找到here的原始代码。