在 PyQt4 中访问 QTextHtmlImporter [英] Accessing QTextHtmlImporter in PyQt4

查看:51
本文介绍了在 PyQt4 中访问 QTextHtmlImporter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyQt4 中编写跨平台应用程序.对于特定功能,我想访问 Qt4 的 QTextHtmlImporter 类.PyQt4 中没有可用的直接 python 适配器类.该类是 src/gui/text/qtextdocumentfragment_p.h 文件的一部分.有什么办法可以在 Python 中访问它吗?

I'm writing a cross platform app in PyQt4. For a particular feature, I would like to access the QTextHtmlImporter class of Qt4. There is no direct python adapter class available in PyQt4. The class is part of the src/gui/text/qtextdocumentfragment_p.h file. Is there any way I can access that in Python?

我想修改QTextDocument.setHtml(),代码为:

void QTextDocument::setHtml(const QString &html) { 
    Q_D(QTextDocument); setUndoRedoEnabled(false); 
    d->clear(); 
    QTextHtmlImporter(this, html).import(); 
    setUndoRedoEnabled(true); 
} 

void QTextDocument::setHtml(const QString &html) { 
    Q_D(QTextDocument); 
    QTextHtmlImporter(this, html).import(); 
} 

基本设置HTML而不清除历史记录.我计划通过使用 PyQt4 的 QTextDocument 的派生类覆盖 setHtml 函数来做到这一点.有没有其他方法可以做到这一点?

Basically setting the HTML without clearing the history. I planned to do this by using a derived class of PyQt4's QTextDocument overriding the setHtml function. Is there any other way to do this?

推荐答案

QTextHtmlImporter 甚至不是 Qt4 API 的一部分,所以简短的回答是:不,没有办法在PyQt4.

QTextHtmlImporter isn't even part of the Qt4 API, so the short answer is: no, there's no way to access it in PyQt4.

当然,您可以尝试将代码移植到 PyQt4,但我猜这将是一项重要的任务.

You could, of course, attempt to port the code to PyQt4, but I'm guessing that would be a non-trivial task.

问题是:为什么你认为你需要这样做?

The question is: why do you think you need to do this?

为什么不能使用 QTextCursor.insertHtmlQTextDocumentFragment.fromHtml?

Why can't you use QTextCursor.insertHtml or QTextDocumentFragment.fromHtml?

编辑

以下是如何在不清除撤消历史记录的情况下设置文本文档中的 html 的示例:

Here's an example of how to set the html in a text document without clearing the undo history:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.edit = QtGui.QTextEdit(self)
        self.undo = QtGui.QPushButton('Undo')
        self.redo = QtGui.QPushButton('Redo')
        self.insert = QtGui.QPushButton('Set Html')
        layout.addWidget(self.edit)
        layout.addWidget(self.undo)
        layout.addWidget(self.redo)
        layout.addWidget(self.insert)
        self.undo.clicked.connect(self.edit.undo)
        self.redo.clicked.connect(self.edit.redo)
        self.insert.clicked.connect(self.handleInsert)
        self.edit.append('One')
        self.edit.append('Two')
        self.edit.append('Three')

    def handleInsert(self):
        cursor = QtGui.QTextCursor(self.edit.document())
        cursor.select(QtGui.QTextCursor.Document)
        cursor.insertHtml("""<p>Some <b>HTML</b> text</p>""")

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

这篇关于在 PyQt4 中访问 QTextHtmlImporter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆