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