⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtextedit.3qt

📁 Linux下的基于X11的图形开发环境。
💻 3QT
📖 第 1 页 / 共 5 页
字号:
Read-only key bindings.TPUsing QTextEdit in LogText Mode.TPUsing QTextEdit as an Editor.TP Editing key bindings.SH "Introduction and Concepts"QTextEdit is an advanced WYSIWYG viewer/editor supporting rich text formatting using HTML-style tags. It is optimized to handle large documents and to respond quickly to user input..PPQTextEdit has three modes of operation: <center>.nf.TSl - l. Mode Command Notes Plain Text Editor setTextFormat(PlainText) Set text with setText(); text() returns plain text. Text attributes (e.g. colors) can be set, but plain text is always returned. Rich Text Editor setTextFormat(RichText) Set text with setText(); text() returns rich text. Rich text editing is fairly limited. You can't set margins or insert images for example (although you can read and correctly display files that have margins set and that include images). This mode is mostly useful for editing small amounts of rich text. Text Viewer setReadOnly(TRUE) Set text with setText() or append() (which has no undo history so is faster and uses less memory); text() returns plain or rich text depending on the textFormat(). This mode can correctly display a large subset of HTML tags. Log Viewer setTextFormat(LogText).TE.fi</center>.PP<sup>1.</sup><small>A more complete API that supports setting margins, images, etc., is planned for a later Qt release.</small>.PPQTextEdit can be used as a syntax highlighting editor when used in conjunction with QSyntaxHighlighter..PPWe recommend that you always call setTextFormat() to set the mode you want to use. If you use AutoText then setText() and append() will try to determine whether the text they are given is plain text or rich text. If you use RichText then setText() and append() will assume that the text they are given is rich text. insert() simply inserts the text it is given..PPQTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, two newlines signify a paragraph. A document consists of zero or more paragraphs, indexed from 0. Characters are indexed on a per-paragraph basis, also indexed from 0. The words in the paragraph are aligned in accordance with the paragraph's alignment(). Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color..PPThe text edit documentation uses the following concepts:.TP\fIcurrent format\fR -- this is the format at the current cursor position, \fIand\fR it is the format of the selected text if any..TP\fIcurrent paragraph\fR -- the paragraph which contains the cursor..PPQTextEdit can display images (using QMimeSourceFactory), lists and tables. If the text is too large to view within the text edit's viewport, scrollbars will appear. The text edit can load both plain text and HTML files (a subset of HTML 3.2 and 4). The rendering style and the set of valid tags are defined by a styleSheet(). Custom tags can be created and placed in a custom style sheet. Change the style sheet with setStyleSheet(); see QStyleSheet for details. The images identified by image tags are displayed if they can be interpreted using the text edit's QMimeSourceFactory; see setMimeSourceFactory()..PPIf you want a text browser with more navigation use QTextBrowser. If you just need to display a small piece of rich text use QLabel or QSimpleRichText..PPIf you create a new QTextEdit, and want to allow the user to edit rich text, call setTextFormat(Qt::RichText) to ensure that the text is treated as rich text. (Rich text uses HTML tags to set text formatting attributes. See QStyleSheet for information on the HTML tags that are supported.). If you don't call setTextFormat() explicitly the text edit will guess from the text itself whether it is rich text or plain text. This means that if the text looks like HTML or XML it will probably be interpreted as rich text, so you should call setTextFormat(Qt::PlainText) to preserve such text..PPNote that we do not intend to add a full-featured web browser widget to Qt (because that would easily double Qt's size and only a few applications would benefit from it). The rich text support in Qt is designed to provide a fast, portable and efficient way to add reasonable online help facilities to applications, and to provide a basis for rich text editors..SH "Using QTextEdit as a Display Widget"QTextEdit can display a large HTML subset, including tables and images..PPThe text is set or replaced using setText() which deletes any existing text and replaces it with the text passed in the setText() call. If you call setText() with legacy HTML (with setTextFormat(RichText) in force), and then call text(), the text that is returned may have different markup, but will render the same. Text can be inserted with insert(), paste(), pasteSubType() and append(). Text that is appended does not go into the undo history; this makes append() faster and consumes less memory. Text can also be cut(). The entire text is deleted with clear() and the selected text is deleted with removeSelectedText(). Selected (marked) text can also be deleted with del() (which will delete the character to the right of the cursor if no text is selected)..PPLoading and saving text is achieved using setText() and text(), for example:.PP.nf.br    QFile file( fileName ); // Read the text from a file.br    if ( file.open( IO_ReadOnly ) ) {.br        QTextStream stream( &file );.br        textEdit->setText( stream.read() );.br    }.br.br    QFile file( fileName ); // Write the text to a file.br    if ( file.open( IO_WriteOnly ) ) {.br        QTextStream stream( &file );.br        stream << textEdit->text();.br        textEdit->setModified( FALSE );.br    }.br.fi.PPBy default the text edit wraps words at whitespace to fit within the text edit widget. The setWordWrap() function is used to specify the kind of word wrap you want, or NoWrap if you don't want any wrapping. Call setWordWrap() to set a fixed pixel width FixedPixelWidth, or character column (e.g. 80 column) FixedColumnWidth with the pixels or columns specified with setWrapColumnOrWidth(). If you use word wrap to the widget's width WidgetWidth, you can specify whether to break on whitespace or anywhere with setWrapPolicy()..PPThe background color is set differently than other widgets, using setPaper(). You specify a brush style which could be a plain color or a complex pixmap..PPHypertext links are automatically underlined; this can be changed with setLinkUnderline(). The tab stop width is set with setTabStopWidth()..PPThe zoomIn() and zoomOut() functions can be used to resize the text by increasing (decreasing for zoomOut()) the point size used. Images are not affected by the zoom functions..PPThe lines() function returns the number of lines in the text and paragraphs() returns the number of paragraphs. The number of lines within a particular paragraph is returned by linesOfParagraph(). The length of the entire text in characters is returned by length()..PPYou can scroll to an anchor in the text, e.g. \fC<a name="anchor">\fR with scrollToAnchor(). The find() function can be used to find and select a given string within the text..PPA read-only QTextEdit provides the same functionality as the (obsolete) QTextView. (QTextView is still supplied for compatibility with old code.).PP<h4> Read-only key bindings </h4>.PPWhen QTextEdit is used read-only the key-bindings are limited to navigation, and text may only be selected with the mouse: <center>.nf.TSl - l. Keypresses Action UpArrow Move one line up DownArrow Move one line down LeftArrow Move one character left RightArrow Move one character right PageUp Move one (viewport) page up PageDown Move one (viewport) page down Home Move to the beginning of the text End Move to the end of the text Shift+Wheel Scroll the page horizontally (the Wheel is the mouse wheel) Ctrl+Wheel.TE.fi</center>.PPThe text edit may be able to provide some meta-information. For example, the documentTitle() function will return the text from within HTML \fC<title>\fR tags..PPThe text displayed in a text edit has a \fIcontext\fR. The context is a path which the text edit's QMimeSourceFactory uses to resolve the locations of files and images. It is passed to the mimeSourceFactory() when quering data. (See QTextEdit() and context().).PP<h4> Using QTextEdit in LogText Mode </h4>.PPSetting the text format to LogText puts the widget in a special mode which is optimized for very large texts. In this mode editing and rich text support are disabled (the widget is explicitly set to read-only mode). This allows the text to be stored in a different, more memory efficient manner. However, a certain degree of text formatting is supported through the use of formatting tags. A tag is delimited by \fC<\fR and \fC>\fR. The characters \fC<\fR, \fC>\fR and \fC&\fR are escaped by using \fC&lt;\fR, \fC&gt;\fR and \fC&amp;\fR. A tag pair consists of a left and a right tag (or open/close tags). Left-tags mark the starting point for formatting, while right-tags mark the ending point. A right-tag always start with a \fC/\fR before the tag keyword. For example \fC<b>\fR and \fC</b>\fR are a tag pair. Tags can be nested, but they have to be closed in the same order as they are opened. For example, \fC<b><u></u></b>\fR is valid, while \fC<b><u></b></u>\fR will output an error message..PPBy using tags it is possible to change the color, bold, italic and underline settings for a piece of text. A color can be specified by using the HTML font tag \fC<font color=colorname>\fR. The color name can be one of the color names from the X11 color database, or a RGB hex value (e.g \fC#00ff00\fR). Example of valid color tags: \fC<font color=red>\fR, \fC<font color="light blue">\fR, \fC<font color="#223344">\fR. Bold, italic and underline settings can be specified by the tags \fC<b>\fR, \fC<i>\fR and \fC<u>\fR. Note that a tag does not necessarily have to be closed. A valid example:.PP.nf.br    This is <font color=red>red</font> while <b>this</b> is <font color=blue>blue</font>..br    <font color=green><font color=yellow>Yellow,</font> and <u>green</u>..br.fi.PPStylesheets can also be used in LogText mode. To create and use a custom tag, you could do the following:.PP.nf.br    QTextEdit * log = new QTextEdit( this );.br    log->setTextFormat( Qt::LogText );.br    QStyleSheetItem * item = new QStyleSheetItem( log->styleSheet(), "mytag" );.br    item->setColor( "red" );.br    item->setFontWeight( QFont::Bold );.br    item->setFontUnderline( TRUE );.br    log->append( "This is a <mytag>custom tag</mytag>!" );.br.fiNote that only the color, bold, underline and italic attributes of a QStyleSheetItem is used in LogText mode..PPThere are a few things that you need to be aware of when the widget is in this mode:.TPFunctions that deal with rich text formatting will not work or return anything valid..TPLines are equivalent to paragraphs..TPInserting lines is not supported. It is only possible to append lines..SH "Using QTextEdit as an Editor"All the information about using QTextEdit as a display widget also applies here..PPThe current format's attributes are set with setItalic(), setBold(), setUnderline(), setFamily() (font family), setPointSize(), setColor() and setCurrentFont(). The current paragraph's alignment is set with setAlignment()..PPUse setSelection() to select text. The setSelectionAttributes() function is used to indicate how selected text should be displayed. Use hasSelectedText() to find out if any text is selected. The currently selected text's position is available using getSelection() and the selected text itself is returned by selectedText(). The selection can be copied to the clipboard with copy(), or cut to the clipboard with cut(). It can be deleted with removeSelectedText(). The entire text can be selected (or unselected) using selectAll(). QTextEdit supports multiple selections. Most of the selection functions operate on the default selection, selection 0. If the user presses a non-selecting key, e.g. a cursor key without also holding down Shift, all selections are cleared..PPSet and get the position of the cursor with setCursorPosition() and getCursorPosition() respectively. When the cursor is moved, the signals currentFontChanged(), currentColorChanged() and currentAlignmentChanged() are emitted to reflect the font, color and alignment at the new cursor position..PPIf the text changes, the textChanged() signal is emitted, and if the user inserts a new line by pressing Return or Enter, returnPressed() is emitted. The isModified() function will return TRUE if the text has been modified..PPQTextEdit provides command-based undo and redo. To set the depth of the command history use setUndoDepth() which defaults to 100 steps. To undo or redo the last operation call undo() or redo(). The signals undoAvailable() and redoAvailable() indicate whether the undo and redo operations can be executed..PPThe indent() function is used to reindent a paragraph. It is useful for code editors, for example in Qt Designer's code editor \fICtrl+I\fR invokes the indent() function..PP<h4> Editing key bindings </h4>.PPThe list of key-bindings which are implemented for editing: <center>.nf.TSl - l. Keypresses Action Backspace Delete the character to the left of the cursor Delete Delete the character to the right of the cursor Ctrl+A Move the cursor to the beginning of the line Ctrl+B Move the cursor one character left Ctrl+C Copy the marked text to the clipboard (also Ctrl+Insert under Windows) Ctrl+D Delete the character to the right of the cursor Ctrl+E Move the cursor to the end of the line Ctrl+F Move the cursor one character right Ctrl+H Delete the character to the left of the cursor Ctrl+K Delete to end of line Ctrl+N Move the cursor one line down Ctrl+P Move the cursor one line up Ctrl+V Paste the clipboard text into line edit (also Shift+Insert under Windows) Ctrl+X Cut the marked text, copy to clipboard (also Shift+Delete under Windows) Ctrl+Z Undo the last operation Ctrl+Y Redo the last operation LeftArrow Move the cursor one character left Ctrl+LeftArrow Move the cursor one word left RightArrow Move the cursor one character right Ctrl+RightArrow Move the cursor one word right UpArrow Move the cursor one line up Ctrl+UpArrow Move the cursor one word up DownArrow Move the cursor one line down Ctrl+Down Arrow Move the cursor one word down PageUp Move the cursor one page up PageDown Move the cursor one page down Home Move the cursor to the beginning of the line Ctrl+Home Move the cursor to the beginning of the text End Move the cursor to the end of the line Ctrl+End Move the cursor to the end of the text Shift+Wheel Scroll the page horizontally (the Wheel is the mouse wheel) Ctrl+Wheel.TE.fi</center>.PPTo select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, \fIShift+Right Arrow\fR will select the character to the right, and \fIShift+Ctrl+Right Arrow\fR will select the word to the right, etc..PPBy default the text edit widget operates in insert mode so all text that the user enters is inserted into the text edit and any text to the right of the cursor is moved out of the way. The mode can be changed to overwrite, where new text overwrites any text to the right of the cursor, using setOverwriteMode()..PPSee also Basic Widgets and Text Related Classes..SS "Member Type Documentation".SH "QTextEdit::AutoFormatting".TP\fCQTextEdit::AutoNone\fR - Do not perform any automatic formatting.TP\fCQTextEdit::AutoBulletList\fR - Only automatically format bulletted lists.TP\fCQTextEdit::AutoAll\fR - Apply all available autoformatting.SH "QTextEdit::CursorAction"This enum is used by moveCursor() to specify in which direction the cursor should be moved:.TP\fCQTextEdit::MoveBackward\fR - Moves the cursor one character backward.TP\fCQTextEdit::MoveWordBackward\fR - Moves the cursor one word backward.TP\fCQTextEdit::MoveForward\fR - Moves the cursor one character forward.TP\fCQTextEdit::MoveWordForward\fR - Moves the cursor one word forward.TP\fCQTextEdit::MoveUp\fR - Moves the cursor up one line.TP\fCQTextEdit::MoveDown\fR - Moves the cursor down one line.TP\fCQTextEdit::MoveLineStart\fR - Moves the cursor to the beginning of the line.TP\fCQTextEdit::MoveLineEnd\fR - Moves the cursor to the end of the line.TP\fCQTextEdit::MoveHome\fR - Moves the cursor to the beginning of the document.TP\fCQTextEdit::MoveEnd\fR - Moves the cursor to the end of the document.TP\fCQTextEdit::MovePgUp\fR - Moves the cursor one viewport page up.TP\fCQTextEdit::MovePgDown\fR - Moves the cursor one viewport page down.SH "QTextEdit::KeyboardAction"This enum is used by doKeyboardAction() to specify which action should be executed:.TP\fCQTextEdit::ActionBackspace\fR - Delete the character to the left of the cursor..TP\fCQTextEdit::ActionDelete\fR - Delete the character to the right of the cursor..TP\fCQTextEdit::ActionReturn\fR - Split the paragraph at the cursor position..TP\fCQTextEdit::ActionKill\fR - If the cursor is not at the end of the paragraph, delete the text from the cursor position until the end of the paragraph. If the cursor is at the end of the paragraph, delete the hard line break at the end of the paragraph: this will cause this paragraph to be joined with the following paragraph..TP\fCQTextEdit::ActionWordBackspace\fR - Delete the word to the left of the cursor position..TP\fCQTextEdit::ActionWordDelete\fR - Delete the word to the right of the cursor position.SH "QTextEdit::VerticalAlignment"This enum is used to set the vertical alignment of the text..TP\fCQTextEdit::AlignNormal\fR - Normal alignment.TP\fCQTextEdit::AlignSuperScript\fR - Superscript.TP\fCQTextEdit::AlignSubScript\fR - Subscript.SH "QTextEdit::WordWrap"This enum defines the QTextEdit's word wrap modes..TP\fCQTextEdit::NoWrap\fR - Do not wrap the text..TP\fCQTextEdit::WidgetWidth\fR - Wrap the text at the current width of the widget (this is the default). Wrapping is at whitespace by default; this can be changed with setWrapPolicy()..TP\fCQTextEdit::FixedPixelWidth\fR - Wrap the text at a fixed number of pixels from the widget's left side. The number of pixels is set with wrapColumnOrWidth()..TP\fCQTextEdit::FixedColumnWidth\fR - Wrap the text at a fixed number of character columns from the widget's left side. The number of characters is set with wrapColumnOrWidth(). This is useful if you need formatted text that can also be displayed gracefully on devices with monospaced fonts, for example a standard VT100 terminal, where you might set wrapColumnOrWidth() to 80..PPSee also wordWrap and wordWrap..SH "QTextEdit::WrapPolicy"This enum defines where text can be wrapped in word wrap mode..TP\fCQTextEdit::AtWhiteSpace\fR - Break lines at whitespace, e.g. spaces or newlines..TP\fCQTextEdit::Anywhere\fR - Break anywhere, including within words..TP\fCQTextEdit::AtWordBoundary\fR - Don't use this deprecated value (it is a synonym for AtWhiteSpace which you should use instead)..TP\fCQTextEdit::AtWordOrDocumentBoundary\fR - Break lines at whitespace, e.g. spaces or newlines if possible. Break it anywhere otherwise..PPSee also wrapPolicy..SH MEMBER FUNCTION DOCUMENTATION.SH "QTextEdit::QTextEdit ( const QString & text, const QString & context = QString::null, QWidget * parent = 0, const char * name = 0 )"Constructs a QTextEdit called \fIname\fR, with parent \fIparent\fR. The text edit will display the text \fItext\fR using context \fIcontext\fR..PPThe \fIcontext\fR is a path which the text edit's QMimeSourceFactory uses to resolve the locations of files and images. It is passed to the mimeSourceFactory() when quering data..PPFor example if the text contains an image tag, \fC<img src="image.png">\fR, and the context is "path/to/look/in", the QMimeSourceFactory will try to load the image from" path/to/look/in/image.png". If the tag was \fC<img src="/image.png">\fR, the context will not be used (because QMimeSourceFactory recognizes that we have used an absolute path) and will try to load "/image.png". The context is applied in exactly the same way to \fIhrefs\fR, for example, \fC<a href="target.html">Target</a>\fR, would resolve to" path/to/look/in/target.html"..SH "QTextEdit::QTextEdit ( QWidget * parent = 0, const char * name = 0 )"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -