| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \page richtext.html
|
|---|
| 44 | \nextpage Rich Text Document Structure
|
|---|
| 45 |
|
|---|
| 46 | \title Rich Text Processing
|
|---|
| 47 | \ingroup architecture
|
|---|
| 48 | \ingroup text-processing
|
|---|
| 49 | \brief An overview of Qt's rich text processing, editing and display features.
|
|---|
| 50 |
|
|---|
| 51 | The Scribe framework provides a set of classes for reading and manipulating
|
|---|
| 52 | structured rich text documents. Unlike previous rich text support in Qt, the
|
|---|
| 53 | new classes are centered around the QTextDocument class rather than raw
|
|---|
| 54 | textual information. This enables the developer to create and modify
|
|---|
| 55 | structured rich text documents without having to prepare content in an
|
|---|
| 56 | intermediate markup format.
|
|---|
| 57 |
|
|---|
| 58 | The information within a document can be accessed via two complementary
|
|---|
| 59 | interfaces: A cursor-based interface is used for editing, and a read-only
|
|---|
| 60 | hierarchical interface provides a high level overview of the document
|
|---|
| 61 | structure. The main advantage of the cursor-based interface is that the
|
|---|
| 62 | text can be edited using operations that mimic a user's interaction with
|
|---|
| 63 | an editor, without losing the underlying structure of the document. The
|
|---|
| 64 | read-only hierarchical interface is most useful when performing operations
|
|---|
| 65 | such as searching and document export.
|
|---|
| 66 |
|
|---|
| 67 | This document is divided up into chapters for convenient reference:
|
|---|
| 68 |
|
|---|
| 69 | \list
|
|---|
| 70 | \i \l{Rich Text Document Structure} outlines
|
|---|
| 71 | the different kinds of elements in a QTextDocument, and describes how
|
|---|
| 72 | they are arranged in a document structure.
|
|---|
| 73 | \i \l{The QTextCursor Interface} explains how rich
|
|---|
| 74 | text documents can be edited using the cursor-based interface.
|
|---|
| 75 | \i \l{Common Rich Text Editing Tasks} examines some
|
|---|
| 76 | common tasks that involve reading or manipulating rich text documents.
|
|---|
| 77 | \i \l{Advanced Rich Text Processing} examines advanced rich text editing tasks.
|
|---|
| 78 | \i \l{Supported HTML Subset} lists the HTML tags supported by QTextDocument.
|
|---|
| 79 | \endlist
|
|---|
| 80 |
|
|---|
| 81 | See also the list of \l{Text Processing Classes}.
|
|---|
| 82 |
|
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | /*!
|
|---|
| 86 | \page richtext-structure.html
|
|---|
| 87 | \contentspage richtext.html Contents
|
|---|
| 88 | \previouspage Rich Text Processing
|
|---|
| 89 | \nextpage The QTextCursor Interface
|
|---|
| 90 |
|
|---|
| 91 | \title Rich Text Document Structure
|
|---|
| 92 |
|
|---|
| 93 | \tableofcontents
|
|---|
| 94 |
|
|---|
| 95 | The structured representation of a text document presents its contents as
|
|---|
| 96 | a hierarchy of text blocks, frames, tables, and other objects. These provide
|
|---|
| 97 | a logical structure to the document and describe how their contents will be
|
|---|
| 98 | displayed. Generally, frames and tables are used to group other
|
|---|
| 99 | structures while text blocks contain the actual textual information.
|
|---|
| 100 |
|
|---|
| 101 | New elements are created and inserted into the document programmatically
|
|---|
| 102 | \l{richtext-cursor.html}{with a QTextCursor} or by using an editor
|
|---|
| 103 | widget, such as QTextEdit. Elements can be given a particular format when
|
|---|
| 104 | they are created; otherwise they take the cursor's current format for the
|
|---|
| 105 | element.
|
|---|
| 106 |
|
|---|
| 107 | \table
|
|---|
| 108 | \row
|
|---|
| 109 | \i \inlineimage richtext-document.png
|
|---|
| 110 | \i \bold{Basic structure}
|
|---|
| 111 |
|
|---|
| 112 | The "top level" of a document might be populated in the way shown.
|
|---|
| 113 | Each document always contains a root frame, and this always contains
|
|---|
| 114 | at least one text block.
|
|---|
| 115 |
|
|---|
| 116 | For documents with some textual content, the root
|
|---|
| 117 | frame usually contains a sequence of blocks and other elements.
|
|---|
| 118 |
|
|---|
| 119 | Sequences of frames and tables are always separated by text blocks in a
|
|---|
| 120 | document, even if the text blocks contain no information. This ensures that
|
|---|
| 121 | new elements can always be inserted between existing structures.
|
|---|
| 122 | \endtable
|
|---|
| 123 |
|
|---|
| 124 | In this chapter, we look at each of the structural elements
|
|---|
| 125 | used in a rich text document, outline their features and uses, and show
|
|---|
| 126 | how to examine their contents. Document editing is described in
|
|---|
| 127 | \l{richtext-cursor.html}{The QTextCursor Interface}.
|
|---|
| 128 |
|
|---|
| 129 | \section1 Rich Text Documents
|
|---|
| 130 |
|
|---|
| 131 | QTextDocument objects contain all the information required to construct
|
|---|
| 132 | rich text documents for use with a QTextEdit widget or in a custom editor.
|
|---|
| 133 | Although QTextEdit makes it easy to display and edit rich text, documents
|
|---|
| 134 | can also be used independently of any editor widget, for example:
|
|---|
| 135 |
|
|---|
| 136 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 0
|
|---|
| 137 |
|
|---|
| 138 | Alternatively, they can be extracted from an existing editor:
|
|---|
| 139 |
|
|---|
| 140 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 1
|
|---|
| 141 |
|
|---|
| 142 | This flexibility enables applications to handle multiple rich text
|
|---|
| 143 | documents without the overhead of multiple editor widgets, or requiring
|
|---|
| 144 | documents to be stored in some intermediate format.
|
|---|
| 145 |
|
|---|
| 146 | An empty document contains a root frame which itself contains a single
|
|---|
| 147 | empty text block. The \l{richtext-cursor.html}{text cursor interface}
|
|---|
| 148 | automatically inserts new document elements into the root frame, and
|
|---|
| 149 | ensures that it is padded with empty blocks where necessary.
|
|---|
| 150 |
|
|---|
| 151 | We obtain the root frame in the following manner:
|
|---|
| 152 |
|
|---|
| 153 | \snippet doc/src/snippets/textdocument-frames/xmlwriter.h 0
|
|---|
| 154 | \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 0
|
|---|
| 155 |
|
|---|
| 156 | When navigating the document structure, it is useful to begin at the
|
|---|
| 157 | root frame because it provides access to the entire document structure.
|
|---|
| 158 |
|
|---|
| 159 | \section1 Document Elements
|
|---|
| 160 |
|
|---|
| 161 | Rich text documents usually consist of common elements such as paragraphs,
|
|---|
| 162 | frames, tables, and lists. These are represented in a QTextDocument
|
|---|
| 163 | by the QTextBlock, QTextFrame, QTextTable, and QTextList classes.
|
|---|
| 164 | Unlike the other elements in a document, images are represented by
|
|---|
| 165 | specially formatted text fragments. This enables them to be placed
|
|---|
| 166 | formatted inline with the surrounding text.
|
|---|
| 167 |
|
|---|
| 168 | The basic structural building blocks in documents are QTextBlock and
|
|---|
| 169 | QTextFrame. Blocks themselves contain fragments of rich text
|
|---|
| 170 | (QTextFragment), but these do not directly influence the high level
|
|---|
| 171 | structure of a document.
|
|---|
| 172 |
|
|---|
| 173 | Elements which can group together other document elements are typically
|
|---|
| 174 | subclasses of QTextObject, and fall into two categories: Elements that
|
|---|
| 175 | group together text blocks are subclasses of QTextBlockGroup, and those
|
|---|
| 176 | that group together frames and other elements are subclasses of QTextFrame.
|
|---|
| 177 |
|
|---|
| 178 | \section2 Text Blocks
|
|---|
| 179 |
|
|---|
| 180 | Text blocks are provided by the QTextBlock class.
|
|---|
| 181 |
|
|---|
| 182 | Text blocks group together fragments of text with different character formats,
|
|---|
| 183 | and are used to represent paragraphs in the document. Each block
|
|---|
| 184 | typically contains a number of text fragments with different styles.
|
|---|
| 185 | Fragments are created when text is inserted into the document, and more
|
|---|
| 186 | of them are added when the document is edited. The document splits, merges,
|
|---|
| 187 | and removes fragments to efficiently represent the different styles
|
|---|
| 188 | of text in the block.
|
|---|
| 189 |
|
|---|
| 190 | The fragments within a given block can be examined by using a
|
|---|
| 191 | QTextBlock::iterator to traverse the block's internal structure:
|
|---|
| 192 |
|
|---|
| 193 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 3
|
|---|
| 194 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 5
|
|---|
| 195 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 6
|
|---|
| 196 |
|
|---|
| 197 | Blocks are also used to represent list items. As a result, blocks can
|
|---|
| 198 | define their own character formats which contain information about
|
|---|
| 199 | block-level decoration, such as the type of bullet points used for
|
|---|
| 200 | list items. The formatting for the block itself is described by the
|
|---|
| 201 | QTextBlockFormat class, and describes properties such as text alignment,
|
|---|
| 202 | indentation, and background color.
|
|---|
| 203 |
|
|---|
| 204 | Although a given document may contain complex structures, once we have a
|
|---|
| 205 | reference to a valid block in the document, we can navigate between each
|
|---|
| 206 | of the text blocks in the order in which they were written:
|
|---|
| 207 |
|
|---|
| 208 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 0
|
|---|
| 209 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 1
|
|---|
| 210 | \snippet doc/src/snippets/textblock-fragments/xmlwriter.cpp 2
|
|---|
| 211 |
|
|---|
| 212 | This method is useful for when you want to extract just the rich text from a
|
|---|
| 213 | document because it ignores frames, tables, and other types of structure.
|
|---|
| 214 |
|
|---|
| 215 | QTextBlock provides comparison operators that make it easier to manipulate
|
|---|
| 216 | blocks: \l{QTextBlock::operator==()}{operator==()} and
|
|---|
| 217 | \l{QTextBlock::operator!=()}{operator!=()} are used to test whether two
|
|---|
| 218 | blocks are the same, and \l{QTextBlock::operator<()}{operator<()} is used
|
|---|
| 219 | to determine which one occurs first in a document.
|
|---|
| 220 |
|
|---|
| 221 | \section2 Frames
|
|---|
| 222 |
|
|---|
| 223 | Frames are provided by the QTextFrame class.
|
|---|
| 224 |
|
|---|
| 225 | Text frames group together blocks of text and child frames, creating
|
|---|
| 226 | document structures that are larger than paragraphs. The format of a frame
|
|---|
| 227 | specifies how it is rendered and positioned on the page. Frames are
|
|---|
| 228 | either inserted into the text flow, or they float on the left or right
|
|---|
| 229 | hand side of the page.
|
|---|
| 230 | Each document contains a root frame that contains all the other document
|
|---|
| 231 | elements. As a result, all frames except the root frame have a parent
|
|---|
| 232 | frame.
|
|---|
| 233 |
|
|---|
| 234 | Since text blocks are used to separate other document elements, each
|
|---|
| 235 | frame will always contain at least one text block, and zero or more
|
|---|
| 236 | child frames. We can inspect the contents of a frame by using a
|
|---|
| 237 | QTextFrame::iterator to traverse the frame's child elements:
|
|---|
| 238 |
|
|---|
| 239 | \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 1
|
|---|
| 240 | \snippet doc/src/snippets/textdocument-frames/xmlwriter.cpp 2
|
|---|
| 241 |
|
|---|
| 242 | Note that the iterator selects both frames and blocks, so it is necessary
|
|---|
| 243 | to check which it is referring to. This allows us to navigate the document
|
|---|
| 244 | structure on a frame-by-frame basis yet still access text blocks if
|
|---|
| 245 | required. Both the QTextBlock::iterator and QTextFrame::iterator classes
|
|---|
| 246 | can be used in complementary ways to extract the required structure from
|
|---|
| 247 | a document.
|
|---|
| 248 |
|
|---|
| 249 | \section2 Tables
|
|---|
| 250 |
|
|---|
| 251 | Tables are provided by the QTextTable class.
|
|---|
| 252 |
|
|---|
| 253 | Tables are collections of cells that are arranged in rows and columns.
|
|---|
| 254 | Each table cell is a document element with its own character format, but it
|
|---|
| 255 | can also contain other elements, such as frames and text blocks. Table cells
|
|---|
| 256 | are automatically created when the table is constructed, or when extra rows
|
|---|
| 257 | or columns are added. They can also be moved between tables.
|
|---|
| 258 |
|
|---|
| 259 | QTextTable is a subclass of QTextFrame, so tables are treated like frames
|
|---|
| 260 | in the document structure. For each frame that we encounter in the
|
|---|
| 261 | document, we can test whether it represents a table, and deal with it in a
|
|---|
| 262 | different way:
|
|---|
| 263 |
|
|---|
| 264 | \snippet doc/src/snippets/textdocument-tables/xmlwriter.cpp 0
|
|---|
| 265 | \snippet doc/src/snippets/textdocument-tables/xmlwriter.cpp 1
|
|---|
| 266 |
|
|---|
| 267 | The cells within an existing table can be examined by iterating through
|
|---|
| 268 | the rows and columns.
|
|---|
| 269 |
|
|---|
| 270 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 9
|
|---|
| 271 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 10
|
|---|
| 272 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 11
|
|---|
| 273 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 12
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | \section2 Lists
|
|---|
| 277 |
|
|---|
| 278 | Lists are provided by the QTextList class.
|
|---|
| 279 |
|
|---|
| 280 | Lists are sequences of text blocks that are formatted in the usual way, but
|
|---|
| 281 | which also provide the standard list decorations such as bullet points and
|
|---|
| 282 | enumerated items. Lists can be nested, and will be indented if the list's
|
|---|
| 283 | format specifies a non-zero indentation.
|
|---|
| 284 |
|
|---|
| 285 | We can refer to each list item by its index in the list:
|
|---|
| 286 |
|
|---|
| 287 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 0
|
|---|
| 288 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 1
|
|---|
| 289 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 2
|
|---|
| 290 |
|
|---|
| 291 | Since QTextList is a subclass of QTextBlockGroup, it does not group the
|
|---|
| 292 | list items as child elements, but instead provides various functions for
|
|---|
| 293 | managing them. This means that any text block we find when traversing a
|
|---|
| 294 | document may actually be a list item. We can ensure that list items are
|
|---|
| 295 | correctly identified by using the following code:
|
|---|
| 296 |
|
|---|
| 297 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 3
|
|---|
| 298 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 4
|
|---|
| 299 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 5
|
|---|
| 300 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 6
|
|---|
| 301 | \snippet doc/src/snippets/textdocument-listitems/mainwindow.cpp 7
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 | \section2 Images
|
|---|
| 305 |
|
|---|
| 306 | Images in QTextDocument are represented by text fragments that reference
|
|---|
| 307 | external images via the resource mechanism. Images are created using the
|
|---|
| 308 | cursor interface, and can be modified later by changing the character
|
|---|
| 309 | format of the image's text fragment:
|
|---|
| 310 |
|
|---|
| 311 | \snippet doc/src/snippets/textdocument-imageformat/main.cpp 0
|
|---|
| 312 | \snippet doc/src/snippets/textdocument-imageformat/main.cpp 1
|
|---|
| 313 | \snippet doc/src/snippets/textdocument-imageformat/main.cpp 2
|
|---|
| 314 |
|
|---|
| 315 | The fragment that represents the image can be found by iterating over
|
|---|
| 316 | the fragments in the text block that contains the image.
|
|---|
| 317 | */
|
|---|
| 318 |
|
|---|
| 319 | /*!
|
|---|
| 320 | \page richtext-cursor.html
|
|---|
| 321 | \contentspage richtext.html Contents
|
|---|
| 322 | \previouspage Rich Text Document Structure
|
|---|
| 323 | \nextpage Common Rich Text Editing Tasks
|
|---|
| 324 |
|
|---|
| 325 | \title The QTextCursor Interface
|
|---|
| 326 |
|
|---|
| 327 | \tableofcontents
|
|---|
| 328 |
|
|---|
| 329 | The QTextCursor interface allows documents and their structure to be
|
|---|
| 330 | edited in a way that should be familiar to most users of text editors and
|
|---|
| 331 | document editing software. Rich text documents can have multiple cursors
|
|---|
| 332 | associated with them, and each of these contains information about their
|
|---|
| 333 | position in the document and any selections that they may hold. This
|
|---|
| 334 | cursor-based paradigm makes common operations, such as cutting and pasting
|
|---|
| 335 | text, simple to implement programmatically, yet it also allows more complex
|
|---|
| 336 | editing operations to be performed on the document.
|
|---|
| 337 |
|
|---|
| 338 | This chapter describes most of the common editing operations that you
|
|---|
| 339 | will need to perform using a cursor, from basic insertion of text and
|
|---|
| 340 | document elements to more complex manipulation of document structures.
|
|---|
| 341 |
|
|---|
| 342 | \section1 Cursor-Based Editing
|
|---|
| 343 |
|
|---|
| 344 | At the simplest level, text documents are made up of a string of characters,
|
|---|
| 345 | marked up in some way to represent the block structure of the text within the
|
|---|
| 346 | document. QTextCursor provides a cursor-based interface that allows the
|
|---|
| 347 | contents of a QTextDocument to be manipulated at the character level. Since
|
|---|
| 348 | the elements (blocks, frames, tables, etc.) are also encoded in the character
|
|---|
| 349 | stream, the document structure can itself be changed by the cursor.
|
|---|
| 350 |
|
|---|
| 351 | The cursor keeps track of its location within its parent document, and can
|
|---|
| 352 | report information about the surrounding structure, such as the enclosing
|
|---|
| 353 | text block, frame, table, or list. The formats of the enclosing structures
|
|---|
| 354 | can also be directly obtained through the cursor.
|
|---|
| 355 |
|
|---|
| 356 | \section2 Using a Cursor
|
|---|
| 357 |
|
|---|
| 358 | The main use of a cursor is to insert or modify text within a block.
|
|---|
| 359 | We can use a text editor's cursor to do this:
|
|---|
| 360 |
|
|---|
| 361 | \snippet doc/src/snippets/textblock-formats/main.cpp 0
|
|---|
| 362 |
|
|---|
| 363 | Alternatively, we can obtain a cursor directly from a document:
|
|---|
| 364 |
|
|---|
| 365 | \snippet doc/src/snippets/textdocument-images/main.cpp 0
|
|---|
| 366 |
|
|---|
| 367 | The cursor is positioned at the start of the document so that we can write
|
|---|
| 368 | into the first (empty) block in the document.
|
|---|
| 369 |
|
|---|
| 370 | \section2 Grouping Cursor Operations
|
|---|
| 371 |
|
|---|
| 372 | A series of editing operations can be packaged together so that they can
|
|---|
| 373 | be replayed, or undone together in a single action. This is achieved by
|
|---|
| 374 | using the \c beginEditBlock() and \c endEditBlock() functions in the
|
|---|
| 375 | following way, as in the following example where we select the word that
|
|---|
| 376 | contains the cursor:
|
|---|
| 377 |
|
|---|
| 378 | \snippet doc/src/snippets/textdocument-selections/mainwindow.cpp 0
|
|---|
| 379 |
|
|---|
| 380 | If editing operations are not grouped, the document automatically records
|
|---|
| 381 | the individual operations so that they can be undone later. Grouping
|
|---|
| 382 | operations into larger packages can make editing more efficient both for
|
|---|
| 383 | the user and for the application, but care has to be taken not to group too
|
|---|
| 384 | many operations together as the user may want find-grained control over the
|
|---|
| 385 | undo process.
|
|---|
| 386 |
|
|---|
| 387 | \section2 Multiple Cursors
|
|---|
| 388 |
|
|---|
| 389 | Multiple cursors can be used to simultaneously edit the same document,
|
|---|
| 390 | although only one will be visible to the user in a QTextEdit widget.
|
|---|
| 391 | The QTextDocument ensures that each cursor writes text correctly and
|
|---|
| 392 | does not interfere with any of the others.
|
|---|
| 393 |
|
|---|
| 394 | \omit
|
|---|
| 395 | \snippet doc/src/snippets/textdocument-cursors/main.cpp 0
|
|---|
| 396 | \snippet doc/src/snippets/textdocument-cursors/main.cpp 1
|
|---|
| 397 | \endomit
|
|---|
| 398 |
|
|---|
| 399 | \section1 Inserting Document Elements
|
|---|
| 400 |
|
|---|
| 401 | QTextCursor provides several functions that can be used to change the
|
|---|
| 402 | structure of a rich text document. Generally, these functions allow
|
|---|
| 403 | document elements to be created with relevant formatting information,
|
|---|
| 404 | and they are inserted into the document at the cursor's position.
|
|---|
| 405 |
|
|---|
| 406 | The first group of functions insert block-level elements, and update the
|
|---|
| 407 | cursor position, but they do not return the element that was inserted:
|
|---|
| 408 |
|
|---|
| 409 | \list
|
|---|
| 410 | \i \l{QTextCursor::insertBlock()}{insertBlock()} inserts a new text block
|
|---|
| 411 | (paragraph) into a document at the cursor's position, and moves the
|
|---|
| 412 | cursor to the start of the new block.
|
|---|
| 413 | \i \l{QTextCursor::insertFragment()}{insertFragment()} inserts an existing
|
|---|
| 414 | text fragment into a document at the cursor's position.
|
|---|
| 415 | \i \l{QTextCursor::insertImage()}{insertImage()} inserts an image into a
|
|---|
| 416 | document at the cursor's position.
|
|---|
| 417 | \i \l{QTextCursor::insertText()}{insertText()} inserts text into the
|
|---|
| 418 | document at the cursor's position.
|
|---|
| 419 | \endlist
|
|---|
| 420 |
|
|---|
| 421 | You can examine the contents of the element that was inserted through the
|
|---|
| 422 | cursor interface.
|
|---|
| 423 |
|
|---|
| 424 | The second group of functions insert elements that provide structure to
|
|---|
| 425 | the document, and return the structure that was inserted:
|
|---|
| 426 |
|
|---|
| 427 | \list
|
|---|
| 428 | \i \l{QTextCursor::insertFrame()}{insertFrame()} inserts a frame into the
|
|---|
| 429 | document \e after the cursor's current block, and moves the cursor to
|
|---|
| 430 | the start of the empty block in the new frame.
|
|---|
| 431 | \i \l{QTextCursor::insertList()}{insertList()} inserts a list into the
|
|---|
| 432 | document at the cursor's position, and moves the cursor to the start
|
|---|
| 433 | of the first item in the list.
|
|---|
| 434 | \i \l{QTextCursor::insertTable()}{insertTable()} inserts a table into
|
|---|
| 435 | the document \e after the cursor's current block, and moves the cursor
|
|---|
| 436 | to the start of the block following the table.
|
|---|
| 437 | \endlist
|
|---|
| 438 |
|
|---|
| 439 | These elements either contain or group together other elements in the
|
|---|
| 440 | document.
|
|---|
| 441 |
|
|---|
| 442 | \section2 Text and Text Fragments
|
|---|
| 443 |
|
|---|
| 444 | Text can be inserted into the current block in the current character
|
|---|
| 445 | format, or in a custom format that is specified with the text:
|
|---|
| 446 |
|
|---|
| 447 | \snippet doc/src/snippets/textdocument-charformats/main.cpp 0
|
|---|
| 448 |
|
|---|
| 449 | Once the character format has been used with a cursor, that format becomes
|
|---|
| 450 | the default format for any text inserted with that cursor until another
|
|---|
| 451 | character format is specified.
|
|---|
| 452 |
|
|---|
| 453 | If a cursor is used to insert text without specifying a character format,
|
|---|
| 454 | the text will be given the character format used at that position in the
|
|---|
| 455 | document.
|
|---|
| 456 |
|
|---|
| 457 | \section2 Blocks
|
|---|
| 458 |
|
|---|
| 459 | Text blocks are inserted into the document with the
|
|---|
| 460 | \l{QTextCursor::insertBlock()}{insertBlock()} function.
|
|---|
| 461 |
|
|---|
| 462 | \snippet doc/src/snippets/textblock-formats/main.cpp 1
|
|---|
| 463 |
|
|---|
| 464 | The cursor is positioned at the start of the new block.
|
|---|
| 465 |
|
|---|
| 466 | \section2 Frames
|
|---|
| 467 |
|
|---|
| 468 | Frames are inserted into a document using the cursor, and will be placed
|
|---|
| 469 | within the cursor's current frame \e after the current block.
|
|---|
| 470 | The following code shows how a frame can be inserted between two text
|
|---|
| 471 | blocks in a document's root frame. We begin by finding the cursor's
|
|---|
| 472 | current frame:
|
|---|
| 473 |
|
|---|
| 474 | \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 0
|
|---|
| 475 |
|
|---|
| 476 | We insert some text in this frame then set up a frame format for the
|
|---|
| 477 | child frame:
|
|---|
| 478 |
|
|---|
| 479 | \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 1
|
|---|
| 480 |
|
|---|
| 481 | The frame format will give the frame an external margin of 32 pixels,
|
|---|
| 482 | internal padding of 8 pixels, and a border that is 4 pixels wide.
|
|---|
| 483 | See the QTextFrameFormat documentation for more information about
|
|---|
| 484 | frame formats.
|
|---|
| 485 |
|
|---|
| 486 | The frame is inserted into the document after the preceding text:
|
|---|
| 487 |
|
|---|
| 488 | \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 2
|
|---|
| 489 |
|
|---|
| 490 | We add some text to the document immediately after we insert the frame.
|
|---|
| 491 | Since the text cursor is positioned \e{inside the frame} when it is inserted
|
|---|
| 492 | into the document, this text will also be inserted inside the frame.
|
|---|
| 493 |
|
|---|
| 494 | Finally, we position the cursor outside the frame by taking the last
|
|---|
| 495 | available cursor position inside the frame we recorded earlier:
|
|---|
| 496 |
|
|---|
| 497 | \snippet doc/src/snippets/textdocument-frames/mainwindow.cpp 3
|
|---|
| 498 |
|
|---|
| 499 | The text that we add last is inserted after the child frame in the
|
|---|
| 500 | document. Since each frame is padded with text blocks, this ensures that
|
|---|
| 501 | more elements can always be inserted with a cursor.
|
|---|
| 502 |
|
|---|
| 503 | \section2 Tables
|
|---|
| 504 |
|
|---|
| 505 | Tables are inserted into the document using the cursor, and will be
|
|---|
| 506 | placed within the cursor's current frame \e after the current block:
|
|---|
| 507 |
|
|---|
| 508 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 0
|
|---|
| 509 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 3
|
|---|
| 510 |
|
|---|
| 511 | Tables can be created with a specific format that defines the overall
|
|---|
| 512 | properties of the table, such as its alignment, background color, and
|
|---|
| 513 | the cell spacing used. It can also determine the constraints on each
|
|---|
| 514 | column, allowing each of them to have a fixed width, or resize according
|
|---|
| 515 | to the available space.
|
|---|
| 516 |
|
|---|
| 517 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 2
|
|---|
| 518 |
|
|---|
| 519 | The columns in the table created above will each take up a certain
|
|---|
| 520 | percentage of the available width. Note that the table format is
|
|---|
| 521 | optional; if you insert a table without a format, some sensible
|
|---|
| 522 | default values will be used for the table's properties.
|
|---|
| 523 |
|
|---|
| 524 | Since cells can contain other document elements, they too can be
|
|---|
| 525 | formatted and styled as necessary.
|
|---|
| 526 |
|
|---|
| 527 | Text can be added to the table by navigating to each cell with the cursor
|
|---|
| 528 | and inserting text.
|
|---|
| 529 |
|
|---|
| 530 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 4
|
|---|
| 531 |
|
|---|
| 532 | We can create a simple timetable by following this approach:
|
|---|
| 533 |
|
|---|
| 534 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 5
|
|---|
| 535 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 6
|
|---|
| 536 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 7
|
|---|
| 537 | \snippet doc/src/snippets/textdocument-tables/mainwindow.cpp 8
|
|---|
| 538 |
|
|---|
| 539 | \section2 Lists
|
|---|
| 540 |
|
|---|
| 541 | Lists of block elements can be automatically created and inserted into the
|
|---|
| 542 | document at the current cursor position. Each list that is created in this
|
|---|
| 543 | way requires a list format to be specified:
|
|---|
| 544 |
|
|---|
| 545 | \snippet doc/src/snippets/textdocument-lists/mainwindow.cpp 0
|
|---|
| 546 |
|
|---|
| 547 | The above code first checks whether the cursor is within an existing list
|
|---|
| 548 | and, if so, gives the list format for the new list a suitable level of
|
|---|
| 549 | indentation. This allows nested lists to be created with increasing
|
|---|
| 550 | levels of indentation. A more sophisticated implementation would also use
|
|---|
| 551 | different kinds of symbol for the bullet points in each level of the list.
|
|---|
| 552 |
|
|---|
| 553 | \section2 Images
|
|---|
| 554 |
|
|---|
| 555 | Inline images are added to documents through the cursor in the usual manner.
|
|---|
| 556 | Unlike many other elements, all of the image properties are specified by the
|
|---|
| 557 | image's format. This means that a QTextImageFormat object has to be
|
|---|
| 558 | created before an image can be inserted:
|
|---|
| 559 |
|
|---|
| 560 | \snippet doc/src/snippets/textdocument-images/main.cpp 1
|
|---|
| 561 |
|
|---|
| 562 | The image name refers to an entry in the application's resource file.
|
|---|
| 563 | The method used to derive this name is described in
|
|---|
| 564 | \l{resources.html}{The Qt Resource System}.
|
|---|
| 565 |
|
|---|
| 566 | */
|
|---|
| 567 |
|
|---|
| 568 | /*!
|
|---|
| 569 | \page richtext-common-tasks.html
|
|---|
| 570 | \contentspage richtext.html Contents
|
|---|
| 571 | \previouspage The QTextCursor Interface
|
|---|
| 572 | \nextpage Advanced Rich Text Processing
|
|---|
| 573 |
|
|---|
| 574 | \title Common Rich Text Editing Tasks
|
|---|
| 575 |
|
|---|
| 576 | \tableofcontents
|
|---|
| 577 |
|
|---|
| 578 | There are a number of tasks that are often performed by developers
|
|---|
| 579 | when editing and processing text documents using Qt. These include the use
|
|---|
| 580 | of display widgets such as QTextBrowser and QTextEdit, creation of
|
|---|
| 581 | documents with QTextDocument, editing using a QTextCursor, and
|
|---|
| 582 | exporting the document structure.
|
|---|
| 583 | This document outlines some of the more common ways of using the rich
|
|---|
| 584 | text classes to perform these tasks, showing convenient patterns that can
|
|---|
| 585 | be reused in your own applications.
|
|---|
| 586 |
|
|---|
| 587 | \section1 Using QTextEdit
|
|---|
| 588 |
|
|---|
| 589 | A text editor widget can be constructed and used to display HTML in the
|
|---|
| 590 | following way:
|
|---|
| 591 |
|
|---|
| 592 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 2
|
|---|
| 593 |
|
|---|
| 594 | By default, the text editor contains a document with a root frame, inside
|
|---|
| 595 | which is an empty text block. This document can be obtained so that it can
|
|---|
| 596 | be modified directly by the application:
|
|---|
| 597 |
|
|---|
| 598 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 3
|
|---|
| 599 |
|
|---|
| 600 | The text editor's cursor may also be used to edit a document:
|
|---|
| 601 |
|
|---|
| 602 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 4
|
|---|
| 603 |
|
|---|
| 604 | Although a document can be edited using many cursors at once, a QTextEdit
|
|---|
| 605 | only displays a single cursor at a time. Therefore, if we want to update the
|
|---|
| 606 | editor to display a particular cursor or its selection, we need to set the
|
|---|
| 607 | editor's cursor after we have modified the document:
|
|---|
| 608 |
|
|---|
| 609 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 5
|
|---|
| 610 |
|
|---|
| 611 | \section1 Selecting Text
|
|---|
| 612 |
|
|---|
| 613 | Text is selected by moving the cursor using operations that are similar to
|
|---|
| 614 | those performed by a user in a text editor. To select text between two
|
|---|
| 615 | points in the document, we need to position the cursor at the first point
|
|---|
| 616 | then move it using a special mode (\l{QTextCursor::MoveMode}) with a
|
|---|
| 617 | move operation (\l{QTextCursor::MoveOperation}).
|
|---|
| 618 | When we select the text, we leave the selection anchor at the old cursor
|
|---|
| 619 | position just as the user might do by holding down the Shift key when
|
|---|
| 620 | selecting text:
|
|---|
| 621 |
|
|---|
| 622 | \snippet doc/src/snippets/textdocument-selections/mainwindow.cpp 1
|
|---|
| 623 |
|
|---|
| 624 | In the above code, a whole word is selected using this method. QTextCursor
|
|---|
| 625 | provides a number of common move operations for selecting individual
|
|---|
| 626 | characters, words, lines, and whole blocks.
|
|---|
| 627 |
|
|---|
| 628 | \section1 Finding Text
|
|---|
| 629 |
|
|---|
| 630 | QTextDocument provides a cursor-based interface for searching, making
|
|---|
| 631 | it easy to find and modify text in the style of a text editor. The following
|
|---|
| 632 | code finds all the instances of a particular word in a document, and changes
|
|---|
| 633 | the color of each:
|
|---|
| 634 |
|
|---|
| 635 | \snippet doc/src/snippets/textdocument-find/main.cpp 0
|
|---|
| 636 | \snippet doc/src/snippets/textdocument-find/main.cpp 1
|
|---|
| 637 |
|
|---|
| 638 | Note that the cursor does not have to be moved after each search and replace
|
|---|
| 639 | operation; it is always positioned at the end of the word that was just
|
|---|
| 640 | replaced.
|
|---|
| 641 |
|
|---|
| 642 | \section1 Printing Documents
|
|---|
| 643 |
|
|---|
| 644 | QTextEdit is designed for the display of large rich text documents that are
|
|---|
| 645 | read on screen, rendering them in the same way as a web browser. As a result,
|
|---|
| 646 | it does not automatically break the contents of the document into page-sized
|
|---|
| 647 | pieces that are suitable for printing.
|
|---|
| 648 |
|
|---|
| 649 | QTextDocument provides a \l{QTextDocument::print()}{print()} function to
|
|---|
| 650 | allow documents to be printed using the QPrinter class. The following code
|
|---|
| 651 | shows how to prepare a document in a QTextEdit for printing with a QPrinter:
|
|---|
| 652 |
|
|---|
| 653 | \snippet doc/src/snippets/textdocument-printing/mainwindow.cpp 0
|
|---|
| 654 |
|
|---|
| 655 | The document is obtained from the text editor, and a QPrinter is constructed
|
|---|
| 656 | then configured using a QPrintDialog. If the user accepts the printer's
|
|---|
| 657 | configuration then the document is formatted and printed using the
|
|---|
| 658 | \l{QTextDocument::print()}{print()} function.
|
|---|
| 659 |
|
|---|
| 660 | */
|
|---|
| 661 |
|
|---|
| 662 | /*!
|
|---|
| 663 | \page richtext-advanced-processing.html
|
|---|
| 664 | \contentspage richtext.html Contents
|
|---|
| 665 | \previouspage Common Rich Text Editing Tasks
|
|---|
| 666 | \nextpage Supported HTML Subset
|
|---|
| 667 |
|
|---|
| 668 | \title Advanced Rich Text Processing
|
|---|
| 669 |
|
|---|
| 670 | \section1 Handling Large Files
|
|---|
| 671 |
|
|---|
| 672 | Qt does not limit the size of files that are used for text
|
|---|
| 673 | processing. In most cases, this will not present a problem. For
|
|---|
| 674 | especially large files, however, you might experience that your
|
|---|
| 675 | application will become unresponsive or that you will run out of
|
|---|
| 676 | memory. The size of the files you can load depends on your
|
|---|
| 677 | hardware and on Qt's and your own application's implementation.
|
|---|
| 678 |
|
|---|
| 679 | If you are faced with this problem, we recommend that you address the
|
|---|
| 680 | following issues:
|
|---|
| 681 |
|
|---|
| 682 | \list
|
|---|
| 683 | \o You should consider breaking up large paragraphs into smaller
|
|---|
| 684 | ones as Qt handles small paragraphs better. You could also
|
|---|
| 685 | insert line breaks at regular intervals, which will look the
|
|---|
| 686 | same as one large paragraph in a QTextEdit.
|
|---|
| 687 | \o You can reduce the amount of blocks in a QTextDocument with
|
|---|
| 688 | \l{QTextDocument::}{maximumBlockCount()}. The document is only
|
|---|
| 689 | as large as the number of blocks as far as QTextEdit is concerned.
|
|---|
| 690 | \o When adding text to a text edit, it is an advantage to add it
|
|---|
| 691 | in an edit block (see example below). The result is that the
|
|---|
| 692 | text edit does not need to build the entire document structure at once.
|
|---|
| 693 | \endlist
|
|---|
| 694 |
|
|---|
| 695 | We give an example of the latter technique from the list. We assume that
|
|---|
| 696 | the text edit is visible.
|
|---|
| 697 |
|
|---|
| 698 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 6
|
|---|
| 699 |
|
|---|
| 700 | \omit
|
|---|
| 701 | Ideas for other sections:
|
|---|
| 702 |
|
|---|
| 703 | * Hiding QTextBlock elements.
|
|---|
| 704 | * Changing the word wrapping mode in QTextEdit. Custom word wrapping?
|
|---|
| 705 | \endomit
|
|---|
| 706 | */
|
|---|
| 707 |
|
|---|
| 708 | /*!
|
|---|
| 709 | \page richtext-html-subset.html
|
|---|
| 710 | \title Supported HTML Subset
|
|---|
| 711 | \brief Describes the support for HTML markup in text widgets.
|
|---|
| 712 |
|
|---|
| 713 | \contentspage richtext.html Contents
|
|---|
| 714 | \previouspage Common Rich Text Editing Tasks
|
|---|
| 715 |
|
|---|
| 716 | Qt's text widgets are able to display rich text, specified using a subset of \l{HTML 4}
|
|---|
| 717 | markup. Widgets that use QTextDocument, such as QLabel, QTextEdit, QTreeWidgetItem and
|
|---|
| 718 | the other item widgets, are able to display rich text specified in this way.
|
|---|
| 719 |
|
|---|
| 720 | \tableofcontents
|
|---|
| 721 |
|
|---|
| 722 | \section1 Using HTML Markup in Text Widgets
|
|---|
| 723 |
|
|---|
| 724 | Widgets automatically detect HTML markup and display rich text accordingly. For example,
|
|---|
| 725 | setting a label's \l{QLabel::}{text} property with the string \c{"<b>Hello</b> <i>Qt!</i>"}
|
|---|
| 726 | will result in the label displaying text like this: \bold{Hello} \e{Qt!}
|
|---|
| 727 |
|
|---|
| 728 | When HTML markup is used for text, Qt follows the rules defined by the \l{HTML 4}
|
|---|
| 729 | specification. This includes default properties for text layout, such as the
|
|---|
| 730 | direction of the text flow (left-to-right) which can be changed by applying the
|
|---|
| 731 | \l{#Block Attributes}{\c dir} attribute to blocks of text.
|
|---|
| 732 |
|
|---|
| 733 | \section1 Supported Tags
|
|---|
| 734 |
|
|---|
| 735 | The following table lists the HTML tags supported by Qt's
|
|---|
| 736 | \l{Rich Text Processing}{rich text} engine:
|
|---|
| 737 |
|
|---|
| 738 | \table
|
|---|
| 739 | \header \o Tag
|
|---|
| 740 | \o Description
|
|---|
| 741 | \o Comment
|
|---|
| 742 | \row \o \c a
|
|---|
| 743 | \o Anchor or link
|
|---|
| 744 | \o Supports the \c href and \c name attributes.
|
|---|
| 745 | \row \o \c address
|
|---|
| 746 | \o Address
|
|---|
| 747 | \o
|
|---|
| 748 | \row \o \c b
|
|---|
| 749 | \o Bold
|
|---|
| 750 | \o
|
|---|
| 751 | \row \o \c big
|
|---|
| 752 | \o Larger font
|
|---|
| 753 | \o
|
|---|
| 754 | \row \o \c blockquote
|
|---|
| 755 | \o Indented paragraph
|
|---|
| 756 | \o
|
|---|
| 757 | \row \o \c body
|
|---|
| 758 | \o Document body
|
|---|
| 759 | \o Supports the \c bgcolor attribute, which
|
|---|
| 760 | can be a Qt \l{QColor::setNamedColor()}{color name}
|
|---|
| 761 | or a \c #RRGGBB color specification.
|
|---|
| 762 | \row \o \c br
|
|---|
| 763 | \o Line break
|
|---|
| 764 | \o
|
|---|
| 765 | \row \o \c center
|
|---|
| 766 | \o Centered paragraph
|
|---|
| 767 | \o
|
|---|
| 768 | \row \o \c cite
|
|---|
| 769 | \o Inline citation
|
|---|
| 770 | \o Same as \c i.
|
|---|
| 771 | \row \o \c code
|
|---|
| 772 | \o Code
|
|---|
| 773 | \o Same as \c tt.
|
|---|
| 774 | \row \o \c dd
|
|---|
| 775 | \o Definition data
|
|---|
| 776 | \o
|
|---|
| 777 | \row \o \c dfn
|
|---|
| 778 | \o Definition
|
|---|
| 779 | \o Same as \c i.
|
|---|
| 780 | \row \o \c div
|
|---|
| 781 | \o Document division
|
|---|
| 782 | \o Supports the standard \l{block attributes}.
|
|---|
| 783 | \row \o \c dl
|
|---|
| 784 | \o Definition list
|
|---|
| 785 | \o Supports the standard \l{block attributes}.
|
|---|
| 786 | \row \o \c dt
|
|---|
| 787 | \o Definition term
|
|---|
| 788 | \o Supports the standard \l{block attributes}.
|
|---|
| 789 | \row \o \c em
|
|---|
| 790 | \o Emphasized
|
|---|
| 791 | \o Same as \c i.
|
|---|
| 792 | \row \o \c font
|
|---|
| 793 | \o Font size, family, and/or color
|
|---|
| 794 | \o Supports the following attributes:
|
|---|
| 795 | \c size, \c face, and \c color (Qt
|
|---|
| 796 | \l{QColor::setNamedColor()}{color names} or
|
|---|
| 797 | \c #RRGGBB).
|
|---|
| 798 | \row \o \c h1
|
|---|
| 799 | \o Level 1 heading
|
|---|
| 800 | \o Supports the standard \l{block attributes}.
|
|---|
| 801 | \row \o \c h2
|
|---|
| 802 | \o Level 2 heading
|
|---|
| 803 | \o Supports the standard \l{block attributes}.
|
|---|
| 804 | \row \o \c h3
|
|---|
| 805 | \o Level 3 heading
|
|---|
| 806 | \o Supports the standard \l{block attributes}.
|
|---|
| 807 | \row \o \c h4
|
|---|
| 808 | \o Level 4 heading
|
|---|
| 809 | \o Supports the standard \l{block attributes}.
|
|---|
| 810 | \row \o \c h5
|
|---|
| 811 | \o Level 5 heading
|
|---|
| 812 | \o Supports the standard \l{block attributes}.
|
|---|
| 813 | \row \o \c h6
|
|---|
| 814 | \o Level 6 heading
|
|---|
| 815 | \o Supports the standard \l{block attributes}.
|
|---|
| 816 | \row \o \c head
|
|---|
| 817 | \o Document header
|
|---|
| 818 | \o
|
|---|
| 819 | \row \o \c hr
|
|---|
| 820 | \o Horizontal line
|
|---|
| 821 | \o Supports the \c width attribute, which can
|
|---|
| 822 | be specified as an absolute or relative (\c %) value.
|
|---|
| 823 | \row \o \c html
|
|---|
| 824 | \o HTML document
|
|---|
| 825 | \o
|
|---|
| 826 | \row \o \c i
|
|---|
| 827 | \o Italic
|
|---|
| 828 | \o
|
|---|
| 829 | \row \o \c img
|
|---|
| 830 | \o Image
|
|---|
| 831 | \o Supports the \c src, \c source
|
|---|
| 832 | (for Qt 3 compatibility), \c width, and \c height
|
|---|
| 833 | attributes.
|
|---|
| 834 | \row \o \c kbd
|
|---|
| 835 | \o User-entered text
|
|---|
| 836 | \o
|
|---|
| 837 | \row \o \c meta
|
|---|
| 838 | \o Meta-information
|
|---|
| 839 | \o If a text encoding is specified using the \c{meta} tag,
|
|---|
| 840 | it is picked up by Qt::codecForHtml().
|
|---|
| 841 | Likewise, if an encoding is specified to
|
|---|
| 842 | QTextDocument::toHtml(), the encoding is stored using
|
|---|
| 843 | a \c meta tag, for example:
|
|---|
| 844 |
|
|---|
| 845 | \snippet doc/src/snippets/code/doc_src_richtext.qdoc 7
|
|---|
| 846 |
|
|---|
| 847 | \row \o \c li
|
|---|
| 848 | \o List item
|
|---|
| 849 | \o
|
|---|
| 850 | \row \o \c nobr
|
|---|
| 851 | \o Non-breakable text
|
|---|
| 852 | \o
|
|---|
| 853 | \row \o \c ol
|
|---|
| 854 | \o Ordered list
|
|---|
| 855 | \o Supports the standard \l{list attributes}.
|
|---|
| 856 | \row \o \c p
|
|---|
| 857 | \o Paragraph
|
|---|
| 858 | \o Left-aligned by default. Supports the standard
|
|---|
| 859 | \l{block attributes}.
|
|---|
| 860 | \row \o \c pre
|
|---|
| 861 | \o Preformated text
|
|---|
| 862 | \o
|
|---|
| 863 | \row \o \c qt
|
|---|
| 864 | \o Qt rich-text document
|
|---|
| 865 | \o Synonym for \c html. Provided for compatibility with
|
|---|
| 866 | earlier versions of Qt.
|
|---|
| 867 | \row \o \c s
|
|---|
| 868 | \o Strikethrough
|
|---|
| 869 | \o
|
|---|
| 870 | \row \o \c samp
|
|---|
| 871 | \o Sample code
|
|---|
| 872 | \o Same as \c tt.
|
|---|
| 873 | \row \o \c small
|
|---|
| 874 | \o Small font
|
|---|
| 875 | \o
|
|---|
| 876 | \row \o \c span
|
|---|
| 877 | \o Grouped elements
|
|---|
| 878 | \o
|
|---|
| 879 | \row \o \c strong
|
|---|
| 880 | \o Strong
|
|---|
| 881 | \o Same as \c b.
|
|---|
| 882 | \row \o \c sub
|
|---|
| 883 | \o Subscript
|
|---|
| 884 | \o
|
|---|
| 885 | \row \o \c sup
|
|---|
| 886 | \o Superscript
|
|---|
| 887 | \o
|
|---|
| 888 | \row \o \c table
|
|---|
| 889 | \o Table
|
|---|
| 890 | \o Supports the following attributes: \c border,
|
|---|
| 891 | \c bgcolor (Qt \l{QColor::setNamedColor()}{color names}
|
|---|
| 892 | or \c #RRGGBB), \c cellspacing, \c cellpadding,
|
|---|
| 893 | \c width (absolute or relative), and \c height.
|
|---|
| 894 | \row \o \c tbody
|
|---|
| 895 | \o Table body
|
|---|
| 896 | \o Does nothing.
|
|---|
| 897 | \row \o \c td
|
|---|
| 898 | \o Table data cell
|
|---|
| 899 | \o Supports the standard \l{table cell attributes}.
|
|---|
| 900 | \row \o \c tfoot
|
|---|
| 901 | \o Table footer
|
|---|
| 902 | \o Does nothing.
|
|---|
| 903 | \row \o \c th
|
|---|
| 904 | \o Table header cell
|
|---|
| 905 | \o Supports the standard \l{table cell attributes}.
|
|---|
| 906 | \row \o \c thead
|
|---|
| 907 | \o Table header
|
|---|
| 908 | \o If the \c thead tag is specified, it is used when printing tables
|
|---|
| 909 | that span multiple pages.
|
|---|
| 910 | \row \o \c title
|
|---|
| 911 | \o Document title
|
|---|
| 912 | \o The value specified using the \c
|
|---|
| 913 | title tag is available through
|
|---|
| 914 | QTextDocument::metaInformation().
|
|---|
| 915 | \row \o \c tr
|
|---|
| 916 | \o Table row
|
|---|
| 917 | \o Supports the \c bgcolor attribute, which
|
|---|
| 918 | can be a Qt \l{QColor::setNamedColor()}{color name}
|
|---|
| 919 | or a \c #RRGGBB color specification.
|
|---|
| 920 | \row \o \c tt
|
|---|
| 921 | \o Typewrite font
|
|---|
| 922 | \o
|
|---|
| 923 | \row \o \c u
|
|---|
| 924 | \o Underlined
|
|---|
| 925 | \o
|
|---|
| 926 | \row \o \c ul
|
|---|
| 927 | \o Unordered list
|
|---|
| 928 | \o Supports the standard \l{list attributes}.
|
|---|
| 929 | \row \o \c var
|
|---|
| 930 | \o Variable
|
|---|
| 931 | \o Same as \c i.
|
|---|
| 932 | \endtable
|
|---|
| 933 |
|
|---|
| 934 | \section1 Block Attributes
|
|---|
| 935 |
|
|---|
| 936 | The following attributes are supported by the \c div, \c dl, \c
|
|---|
| 937 | dt, \c h1, \c h2, \c h3, \c h4, \c h5, \c h6, \c p tags:
|
|---|
| 938 |
|
|---|
| 939 | \list
|
|---|
| 940 | \o \c align (\c left, \c right, \c center, \c justify)
|
|---|
| 941 | \o \c dir (\c ltr, \c rtl)
|
|---|
| 942 | \endlist
|
|---|
| 943 |
|
|---|
| 944 | \section1 List Attributes
|
|---|
| 945 |
|
|---|
| 946 | The following attribute is supported by the \c ol and \c ul tags:
|
|---|
| 947 |
|
|---|
| 948 | \list
|
|---|
| 949 | \o \c type (\c 1, \c a, \c A, \c square, \c disc, \c circle)
|
|---|
| 950 | \endlist
|
|---|
| 951 |
|
|---|
| 952 | \section1 Table Cell Attributes
|
|---|
| 953 |
|
|---|
| 954 | The following attributes are supported by the \c td and \c th
|
|---|
| 955 | tags:
|
|---|
| 956 |
|
|---|
| 957 | \list
|
|---|
| 958 | \o \c width (absolute, relative, or no-value)
|
|---|
| 959 | \o \c bgcolor (Qt \l{QColor::setNamedColor()}{color names} or \c #RRGGBB)
|
|---|
| 960 | \o \c colspan
|
|---|
| 961 | \o \c rowspan
|
|---|
| 962 | \o \c align (\c left, \c right, \c center, \c justify)
|
|---|
| 963 | \o \c valign (\c top, \c middle, \c bottom)
|
|---|
| 964 | \endlist
|
|---|
| 965 |
|
|---|
| 966 | \section1 CSS Properties
|
|---|
| 967 | The following table lists the CSS properties supported by Qt's
|
|---|
| 968 | \l{Rich Text Processing}{rich text} engine:
|
|---|
| 969 |
|
|---|
| 970 | \table
|
|---|
| 971 | \header \o Property
|
|---|
| 972 | \o Values
|
|---|
| 973 | \o Description
|
|---|
| 974 | \row
|
|---|
| 975 | \o \c background-color
|
|---|
| 976 | \o <color>
|
|---|
| 977 | \o Background color for elements
|
|---|
| 978 | \row
|
|---|
| 979 | \o \c background-image
|
|---|
| 980 | \o <uri>
|
|---|
| 981 | \o Background image for elements
|
|---|
| 982 | \row \o \c color
|
|---|
| 983 | \o <color>
|
|---|
| 984 | \o Text foreground color
|
|---|
| 985 | \row \o \c font-family
|
|---|
| 986 | \o <family name>
|
|---|
| 987 | \o Font family name
|
|---|
| 988 | \row \o \c font-size
|
|---|
| 989 | \o [ small | medium | large | x-large | xx-large ] | <size>pt | <size>px
|
|---|
| 990 | \o Font size relative to the document font, or specified in points or pixels
|
|---|
| 991 | \row \o \c font-style
|
|---|
| 992 | \o [ normal | italic | oblique ]
|
|---|
| 993 | \o
|
|---|
| 994 | \row \o \c font-weight
|
|---|
| 995 | \o [ normal | bold | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 ]
|
|---|
| 996 | \o Specifies the font weight used for text, where \c normal and \c bold
|
|---|
| 997 | are mapped to the corresponding QFont weights. Numeric values are
|
|---|
| 998 | 8 times the equivalent QFont weight values.
|
|---|
| 999 | \row \o \c text-decoration
|
|---|
| 1000 | \o none | [ underline || overline || line-through ]
|
|---|
| 1001 | \o Additional text effects
|
|---|
| 1002 | \row \o \c font
|
|---|
| 1003 | \o [ [ <'font-style'> || <'font-weight'> ]? <'font-size'> <'font-family'> ]
|
|---|
| 1004 | \o Font shorthand property
|
|---|
| 1005 | \row \o \c text-indent
|
|---|
| 1006 | \o <length>px
|
|---|
| 1007 | \o First line text indentation in pixels
|
|---|
| 1008 | \row \o \c white-space
|
|---|
| 1009 | \o normal | pre | nowrap | pre-wrap
|
|---|
| 1010 | \o Declares how whitespace in HTML is handled.
|
|---|
| 1011 | \row \o \c margin-top
|
|---|
| 1012 | \o <length>px
|
|---|
| 1013 | \o Top paragraph margin in pixels
|
|---|
| 1014 | \row \o \c margin-bottom
|
|---|
| 1015 | \o <length>px
|
|---|
| 1016 | \o Bottom paragraph margin in pixels
|
|---|
| 1017 | \row \o \c margin-left
|
|---|
| 1018 | \o <length>px
|
|---|
| 1019 | \o Left paragraph margin in pixels
|
|---|
| 1020 | \row \o \c margin-right
|
|---|
| 1021 | \o <length>px
|
|---|
| 1022 | \o Right paragraph margin in pixels
|
|---|
| 1023 | \row \o \c padding-top
|
|---|
| 1024 | \o <length>px
|
|---|
| 1025 | \o Top table cell padding in pixels
|
|---|
| 1026 | \row \o \c padding-bottom
|
|---|
| 1027 | \o <length>px
|
|---|
| 1028 | \o Bottom table cell padding in pixels
|
|---|
| 1029 | \row \o \c padding-left
|
|---|
| 1030 | \o <length>px
|
|---|
| 1031 | \o Left table cell padding in pixels
|
|---|
| 1032 | \row \o \c padding-right
|
|---|
| 1033 | \o <length>px
|
|---|
| 1034 | \o Right table cell padding in pixels
|
|---|
| 1035 | \row \o \c padding
|
|---|
| 1036 | \o <length>px
|
|---|
| 1037 | \o Shorthand for setting all the padding properties at once.
|
|---|
| 1038 | \row \o \c vertical-align
|
|---|
| 1039 | \o baseline | sub | super | middle | top | bottom
|
|---|
| 1040 | \o Vertical text alignment. For vertical alignment in text table cells only middle, top, and bottom apply.
|
|---|
| 1041 | \row \o \c border-color
|
|---|
| 1042 | \o <color>
|
|---|
| 1043 | \o Border color for text tables.
|
|---|
| 1044 | \row \o \c border-style
|
|---|
| 1045 | \o none | dotted | dashed | dot-dash | dot-dot-dash | solid | double | groove | ridge | inset | outset
|
|---|
| 1046 | \o Border style for text tables.
|
|---|
| 1047 | \row \o \c background
|
|---|
| 1048 | \o [ <'background-color'> || <'background-image'> ]
|
|---|
| 1049 | \o Background shorthand property
|
|---|
| 1050 | \row \o \c page-break-before
|
|---|
| 1051 | \o [ auto | always ]
|
|---|
| 1052 | \o Make it possible to enforce a page break before the paragraph/table
|
|---|
| 1053 | \row \o \c page-break-after
|
|---|
| 1054 | \o [ auto | always ]
|
|---|
| 1055 | \o Make it possible to enforce a page break after the paragraph/table
|
|---|
| 1056 | \row \o float
|
|---|
| 1057 | \o [ left | right | none ]
|
|---|
| 1058 | \o Specifies where an image or a text will be placed in another element. Note that the \c float property is
|
|---|
| 1059 | only supported for tables and images.
|
|---|
| 1060 | \row \o \c text-transform
|
|---|
| 1061 | \o [ uppercase | lowercase | smallcaps ]
|
|---|
| 1062 | \o Select the transformation that will be performed on the text prior to displaying it.
|
|---|
| 1063 | \row \o \c word-spacing
|
|---|
| 1064 | \o <width>px
|
|---|
| 1065 | \o Specifies an alternate spacing between each word.
|
|---|
| 1066 | \endtable
|
|---|
| 1067 |
|
|---|
| 1068 | \section1 Supported CSS Selectors
|
|---|
| 1069 |
|
|---|
| 1070 | All CSS 2.1 selector classes are supported except pseudo-class selectors such
|
|---|
| 1071 | as \c{:first-child}, \c{:visited} and \c{:hover}.
|
|---|
| 1072 |
|
|---|
| 1073 | */
|
|---|