在 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.insertHtml 或
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_())