Common Rich Text Editing Tasks
There are a number of tasks that are often performed by developers when editing and processing text documents using Qt. These include the use of display widgets such as QTextBrowser and QTextEdit, creation of documents with QTextDocument, editing using a QTextCursor, and exporting the document structure. This document outlines some of the more common ways of using the rich text classes to perform these tasks, showing convenient patterns that can be reused in your own applications. Using QTextEditA text editor widget can be constructed and used to display HTML in the following way: QTextEdit *editor = new QTextEdit(parent); editor->setHtml(aStringContainingHTMLtext); editor->show(); By default, the text editor contains a document with a root frame, inside which is an empty text block. This document can be obtained so that it can be modified directly by the application: QTextDocument *document = editor->document(); The text editor's cursor may also be used to edit a document: QTextCursor cursor = editor->textCursor(); Although a document can be edited using many cursors at once, a QTextEdit only displays a single cursor at a time. Therefore, if we want to update the editor to display a particular cursor or its selection, we need to set the editor's cursor after we have modified the document: editor->setTextCursor(cursor); Selecting TextText is selected by moving the cursor using operations that are similar to those performed by a user in a text editor. To select text between two points in the document, we need to position the cursor at the first point then move it using a special mode (QTextCursor::MoveMode) with a move operation (QTextCursor::MoveOperation). When we select the text, we leave the selection anchor at the old cursor position just as the user might do by holding down the Shift key when selecting text: cursor.movePosition(QTextCursor::StartOfWord); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); In the above code, a whole word is selected using this method. QTextCursor provides a number of common move operations for selecting individual characters, words, lines, and whole blocks. Finding TextQTextDocument provides a cursor-based interface for searching, making it easy to find and modify text in the style of a text editor. The following code finds all the instances of a particular word in a document, and changes the color of each: QTextCursor newCursor(document); while (!newCursor.isNull() && !newCursor.atEnd()) { newCursor = document->find(searchString, newCursor); if (!newCursor.isNull()) { newCursor.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor); newCursor.mergeCharFormat(colorFormat); } } Note that the cursor does not have to be moved after each search and replace operation; it is always positioned at the end of the word that was just replaced. Printing DocumentsQTextEdit is designed for the display of large rich text documents that are read on screen, rendering them in the same way as a web browser. As a result, it does not automatically break the contents of the document into page-sized pieces that are suitable for printing. QTextDocument provides a print() function to allow documents to be printed using the QPrinter class. The following code shows how to prepare a document in a QTextEdit for printing with a QPrinter: QTextDocument *document = editor->document(); QPrinter printer; QPrintDialog *dlg = new QPrintDialog(&printer, this); if (dlg->exec() != QDialog::Accepted) return; document->print(&printer); The document is obtained from the text editor, and a QPrinter is constructed then configured using a QPrintDialog. If the user accepts the printer's configuration then the document is formatted and printed using the print() function. [Previous: The QTextCursor Interface] [Contents] [Next: Supported HTML Subset] |
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.1 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com