PyQt5 QTextEdit在编辑时进行实时HTML预览

时间:2019-06-12 21:46:56

标签: python html pyqt5 editor qtextedit

我正在尝试编辑一个名为self.chatTextField的QTextEdit,以实时格式显示HTML预览。例如,如果我执行“”,则希望它不显示标签,而将文本加粗。如果我将textBrowswer用作预览器,则可以完成这项工作,但我希望所有这些都在一个小部件中。

enter image description here

如上图所示,我想匹配将HTML显示为最终产品而不是标签的部分。这可能吗?

我尝试了多种方法来转换.toPlainText(),然后添加HTML页眉和页脚。我尝试过.insertHtml()和.setHtml()。我试图用打开和关闭的胡萝卜来做花样,但似乎没有任何效果。我要么只是简单地删除所有HTML(带有胡萝卜/标签的所有内容),要么就会导致递归错误。

这是处理更新HTML的唯一代码部分。如果您需要更多代码,请告诉我。我也可以提供UI文件。

def printMaster(self):
    global my_text

    if self.chatTextField.toPlainText() != "":
        oldText = self.chatTextField.toPlainText()
        newText = self.textBrowser.toPlainText()
        if oldText != newText and oldText != my_text:
            self.textBrowser.setText(self.chatTextField.toPlainText())
            my_text = self.chatTextField.toPlainText()
            if my_text.endswith('>'):
                #my_text = self.chatTextField.toHtml()

                # self.chatTextField.clear()
                print(my_text)
                # self.chatTextField.setHtml(my_text)
                QCoreApplication.processEvents()



            # cursor = self.chatTextField.textCursor()
            # pos = cursor.selectionEnd()
            #
            # print(self.chatTextField.toHtml())
            #
            # my_text = self.chatTextField.toHtml()
            #
            # self.chatTextField.clear()
            # # self.chatTextField.insertHtml(str(my_text))
            #
            # if my_text.endswith('<'):
            #     my_text = my_text + "<"
            #     self.chatTextField.setHtml(my_text)
            #     print(True)
            # else:
            #     self.chatTextField.insertHtml(my_text)
            #
            #
            # cursor.setPosition(pos)
            #
            # self.chatTextField.setTextCursor(cursor)
            #
            #
            # QCoreApplication.processEvents()
            #
            # # self.chatTextField.show()

0 个答案:

没有答案
相关问题