Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Text Object Example#
The Text Object example shows how to insert an SVG file into a QTextDocument
.
A QTextDocument
consists of a hierarchy of elements, such as text blocks and frames. A text object describes the structure or format of one or more of these elements. For instance, images imported from HTML are implemented using text objects. Text objects are used by the document’s layout
to lay out and render (paint) the document. Each object knows how to paint the elements they govern, and calculates their size.
To be able to insert an SVG image into a text document, we create a text object, and implement painting for that object. This object can then be set
on a QTextCharFormat
. We also register the text object with the layout of the document, enabling it to draw QTextCharFormat
s governed by our text object. We can summarize the procedure with the following steps:
Implement the text object.
Register the text object with the layout of the text document.
Set the text object on a
QTextCharFormat
.Insert a
ObjectReplacementCharacter
with that text char format into the document.
The example consists of the following classes:
SvgTextObject
implements the text object.
Window
shows aQTextEdit
into which SVG images can be inserted.
SvgTextObject Class Definition#
Let’s take a look at the header file of SvgTextObject
:
class SvgTextObject(QObject, QTextObjectInterface): Q_OBJECT Q_INTERFACES(QTextObjectInterface) # public QSizeF intrinsicSize(QTextDocument doc, int posInDocument, QTextFormat format) override def drawObject(painter, rect, doc,): int posInDocument, QTextFormat format) override
A text object is a QObject
that implements QTextObjectInterface
. Note that the first class inherited must be QObject
, and that you must use Q_INTERFACES
to let Qt know that your class implements QTextObjectInterface
.
The document layout keeps a collection of text objects stored as QObject
s, each of which has an associated object type. The layout casts the QObject
for the associated object type into the QTextObjectInterface
.
The intrinsicSize()
and drawObject()
functions are then used to calculate the size of the text object and draw it.
SvgTextObject Class Implementation#
We start of by taking a look at the intrinsicSize()
function:
QSizeF SvgTextObject.intrinsicSize(QTextDocument * /*doc*/, int /*posInDocument*/, QTextFormat format) bufferedImage = QImage(format.property(Window::SvgData)) size = bufferedImage.size() if size.height() > 25: = 25.0 / (double) size.height() return QSizeF(size)
intrinsicSize()
is called by the layout to calculate the size of the text object. Notice that we have drawn the SVG image on a QImage
. This is because SVG rendering is quite expensive. The example would lag seriously for large images if we drew them with a QSvgRenderer
each time.
def drawObject(self, painter, rect,): QTextDocument * /*doc*/, int /*posInDocument*/, QTextFormat format) bufferedImage = QImage(format.property(Window::SvgData)) painter.drawImage(rect, bufferedImage)
In drawObject()
, we paint the SVG image using the QPainter
provided by the layout.
Window Class Definition#
The Window
class is a self-contained window that has a QTextEdit
in which SVG images can be inserted.
class Window(QWidget): Q_OBJECT # public enum { SvgTextFormat = QTextFormat.UserObject + 1 } SvgProperties = { SvgData = 1 } Window() # private slots def insertTextObject(): # private def setupTextObject(): def setupGui(): # private textEdit = QTextEdit() fileNameLabel = QLabel() fileNameLineEdit = QLineEdit() insertTextObjectButton = QPushButton()
The insertTextObject()
slot inserts an SVG image at the current cursor position, while setupTextObject()
creates and registers the SvgTextObject with the layout of the text edit’s document.
The constructor simply calls setupTextObject()
and setupGui()
, which creates and lays out the widgets of the Window
.
Window Class Implementation#
We will now take a closer look at the functions that are relevant to our text object, starting with the setupTextObject()
function.
def setupTextObject(self): svgInterface = SvgTextObject() svgInterface.setParent(self) textEdit.document().documentLayout().registerHandler(SvgTextFormat, svgInterface)
SvgTextFormat
's value is the number of our object type. It is used to identify object types by the document layout.
Note that we only create one SvgTextObject instance; it will be used for all QTextCharFormat
‘s with the SvgTextFormat
object type.
Let’s move on to the insertTextObject()
function:
def insertTextObject(self): fileName = fileNameLineEdit.text() file = QFile(fileName) if not file.open(QIODevice.ReadOnly): QMessageBox.warning(self, tr("Error Opening File"), tr("Could not open '%1'").arg(fileName)) svgData = file.readAll()
First, the .svg
file is opened and its contents are read into the svgData
array.
svgCharFormat = QTextCharFormat() svgCharFormat.setObjectType(SvgTextFormat) renderer = QSvgRenderer(svgData) svgBufferImage = QImage(renderer.defaultSize(), QImage.Format_ARGB32) painter = QPainter(svgBufferImage) renderer.render(painter, svgBufferImage.rect()) svgCharFormat.setProperty(SvgData, svgBufferImage) cursor = textEdit.textCursor() cursor.insertText(QString(QChar.ObjectReplacementCharacter), svgCharFormat) textEdit.setTextCursor(cursor)
To speed things up, we buffer the SVG image in a QImage
. We use setProperty()
to store the QImage
in the in the QTextCharFormat
. We can retrieve it later with property()
.
We insert the char format in the standard way - using a QTextCursor
. Notice that we use the special QChar
ObjectReplacementCharacter
.