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