![]() |
Qt 6.x
The Qt SDK
|
The QTextBrowser class provides a rich text browser with hypertext navigation. More...
#include <qtextbrowser.h>
Public Slots | |
void | setSource (const QUrl &name, QTextDocument::ResourceType type=QTextDocument::UnknownResource) |
Attempts to load the document at the given url with the specified type. | |
virtual void | backward () |
Changes the document displayed to the previous document in the list of documents built by navigating links. | |
virtual void | forward () |
Changes the document displayed to the next document in the list of documents built by navigating links. | |
virtual void | home () |
Changes the document displayed to be the first document from the history. | |
virtual void | reload () |
Reloads the current set source. | |
![]() | |
void | setFontPointSize (qreal s) |
Sets the point size of the current format to s. | |
void | setFontFamily (const QString &fontFamily) |
Sets the font family of the current format to fontFamily. | |
void | setFontWeight (int w) |
Sets the font weight of the current format to the given weight, where the value used is in the range defined by the QFont::Weight enum. | |
void | setFontUnderline (bool b) |
If underline is true, sets the current format to underline; otherwise sets the current format to non-underline. | |
void | setFontItalic (bool b) |
If italic is true, sets the current format to italic; otherwise sets the current format to non-italic. | |
void | setTextColor (const QColor &c) |
Sets the text color of the current format to c. | |
void | setTextBackgroundColor (const QColor &c) |
void | setCurrentFont (const QFont &f) |
Sets the font of the current format to f. | |
void | setAlignment (Qt::Alignment a) |
Sets the alignment of the current paragraph to a. | |
void | setPlainText (const QString &text) |
Changes the text of the text edit to the string text. | |
void | setHtml (const QString &text) |
void | setText (const QString &text) |
void | cut () |
Copies the selected text to the clipboard and deletes it from the text edit. | |
void | copy () |
Copies any selected text to the clipboard. | |
void | paste () |
Pastes the text from the clipboard into the text edit at the current cursor position. | |
void | undo () |
void | redo () |
void | clear () |
Deletes all the text in the text edit. | |
void | selectAll () |
Selects all text. | |
void | insertPlainText (const QString &text) |
Convenience slot that inserts text at the current cursor position. | |
void | insertHtml (const QString &text) |
Convenience slot that inserts text which is assumed to be of html formatting at the current cursor position. | |
void | append (const QString &text) |
Appends a new paragraph with text to the end of the text edit. | |
void | scrollToAnchor (const QString &name) |
Scrolls the text edit so that the anchor with the given name is visible; does nothing if the name is empty, or is already visible, or isn't found. | |
void | zoomIn (int range=1) |
Zooms in on the text by making the base font size range points larger and recalculating all font sizes to be the new size. | |
void | zoomOut (int range=1) |
Zooms out on the text by making the base font size range points smaller and recalculating all font sizes to be the new size. | |
Signals | |
void | backwardAvailable (bool) |
This signal is emitted when the availability of backward() changes. | |
void | forwardAvailable (bool) |
This signal is emitted when the availability of forward() changes. | |
void | historyChanged () |
void | sourceChanged (const QUrl &) |
This signal is emitted when the source has changed, src being the new source. | |
void | highlighted (const QUrl &) |
This signal is emitted when the user has selected but not activated an anchor in the document. | |
void | anchorClicked (const QUrl &) |
This signal is emitted when the user clicks an anchor. | |
![]() | |
void | textChanged () |
This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied. | |
void | undoAvailable (bool b) |
This signal is emitted whenever undo operations become available (available is true) or unavailable (available is false). | |
void | redoAvailable (bool b) |
This signal is emitted whenever redo operations become available (available is true) or unavailable (available is false). | |
void | currentCharFormatChanged (const QTextCharFormat &format) |
This signal is emitted if the current character format has changed, for example caused by a change of the cursor position. | |
void | copyAvailable (bool b) |
This signal is emitted when text is selected or de-selected in the text edit. | |
void | selectionChanged () |
This signal is emitted whenever the selection changes. | |
void | cursorPositionChanged () |
This signal is emitted whenever the position of the cursor changed. | |
Public Member Functions | |
QTextBrowser (QWidget *parent=nullptr) | |
Constructs an empty QTextBrowser with parent parent. | |
virtual | ~QTextBrowser () |
QUrl | source () const |
QTextDocument::ResourceType | sourceType () const |
QStringList | searchPaths () const |
void | setSearchPaths (const QStringList &paths) |
virtual QVariant | loadResource (int type, const QUrl &name) override |
This function is called when the document is loaded and for each image in the document. | |
bool | isBackwardAvailable () const |
bool | isForwardAvailable () const |
void | clearHistory () |
QString | historyTitle (int) const |
Returns the documentTitle() of the HistoryItem. | |
QUrl | historyUrl (int) const |
Returns the url of the HistoryItem. | |
int | backwardHistoryCount () const |
Returns the number of locations backward in the history. | |
int | forwardHistoryCount () const |
Returns the number of locations forward in the history. | |
bool | openExternalLinks () const |
void | setOpenExternalLinks (bool open) |
bool | openLinks () const |
void | setOpenLinks (bool open) |
![]() | |
QTextEdit (QWidget *parent=nullptr) | |
Constructs an empty QTextEdit with parent parent. | |
QTextEdit (const QString &text, QWidget *parent=nullptr) | |
Constructs a QTextEdit with parent parent. | |
virtual | ~QTextEdit () |
Destructor. | |
void | setDocument (QTextDocument *document) |
QTextDocument * | document () const |
void | setPlaceholderText (const QString &placeholderText) |
QString | placeholderText () const |
void | setTextCursor (const QTextCursor &cursor) |
Sets the visible cursor. | |
QTextCursor | textCursor () const |
Returns a copy of the QTextCursor that represents the currently visible cursor. | |
bool | isReadOnly () const |
void | setReadOnly (bool ro) |
void | setTextInteractionFlags (Qt::TextInteractionFlags flags) |
Qt::TextInteractionFlags | textInteractionFlags () const |
qreal | fontPointSize () const |
Returns the point size of the font of the current format. | |
QString | fontFamily () const |
Returns the font family of the current format. | |
int | fontWeight () const |
Returns the font weight of the current format. | |
bool | fontUnderline () const |
Returns true if the font of the current format is underlined; otherwise returns false. | |
bool | fontItalic () const |
Returns true if the font of the current format is italic; otherwise returns false. | |
QColor | textColor () const |
Returns the text color of the current format. | |
QColor | textBackgroundColor () const |
QFont | currentFont () const |
Returns the font of the current format. | |
Qt::Alignment | alignment () const |
Returns the alignment of the current paragraph. | |
void | mergeCurrentCharFormat (const QTextCharFormat &modifier) |
Merges the properties specified in modifier into the current character format by calling QTextCursor::mergeCharFormat on the editor's cursor. | |
void | setCurrentCharFormat (const QTextCharFormat &format) |
Sets the char format that is be used when inserting new text to format by calling QTextCursor::setCharFormat() on the editor's cursor. | |
QTextCharFormat | currentCharFormat () const |
Returns the char format that is used when inserting new text. | |
AutoFormatting | autoFormatting () const |
void | setAutoFormatting (AutoFormatting features) |
bool | tabChangesFocus () const |
void | setTabChangesFocus (bool b) |
void | setDocumentTitle (const QString &title) |
QString | documentTitle () const |
bool | isUndoRedoEnabled () const |
void | setUndoRedoEnabled (bool enable) |
LineWrapMode | lineWrapMode () const |
void | setLineWrapMode (LineWrapMode mode) |
int | lineWrapColumnOrWidth () const |
void | setLineWrapColumnOrWidth (int w) |
QTextOption::WrapMode | wordWrapMode () const |
the mode QTextEdit will use when wrapping text by words | |
void | setWordWrapMode (QTextOption::WrapMode policy) |
bool | find (const QString &exp, QTextDocument::FindFlags options=QTextDocument::FindFlags()) |
Finds the next occurrence of the string, exp, using the given options. | |
QString | toPlainText () const |
QString QTextEdit::toPlainText() const. | |
QString | toHtml () const |
void | ensureCursorVisible () |
Ensures that the cursor is visible by scrolling the text edit if necessary. | |
virtual Q_INVOKABLE QVariant | loadResource (int type, const QUrl &name) |
Loads the resource specified by the given type and name. | |
QMenu * | createStandardContextMenu () |
\reimp | |
QMenu * | createStandardContextMenu (const QPoint &position) |
QTextCursor | cursorForPosition (const QPoint &pos) const |
returns a QTextCursor at position pos (in viewport coordinates). | |
QRect | cursorRect (const QTextCursor &cursor) const |
returns a rectangle (in viewport coordinates) that includes the cursor. | |
QRect | cursorRect () const |
returns a rectangle (in viewport coordinates) that includes the cursor of the text edit. | |
QString | anchorAt (const QPoint &pos) const |
Returns the reference of the anchor at position pos, or an empty string if no anchor exists at that point. | |
bool | overwriteMode () const |
void | setOverwriteMode (bool overwrite) |
qreal | tabStopDistance () const |
void | setTabStopDistance (qreal distance) |
int | cursorWidth () const |
void | setCursorWidth (int width) |
bool | acceptRichText () const |
void | setAcceptRichText (bool accept) |
void | setExtraSelections (const QList< ExtraSelection > &selections) |
\variable QTextEdit::ExtraSelection::cursor A cursor that contains a selection in a QTextDocument | |
QList< ExtraSelection > | extraSelections () const |
void | moveCursor (QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode=QTextCursor::MoveAnchor) |
bool | canPaste () const |
void | print (QPagedPaintDevice *printer) const |
QVariant | inputMethodQuery (Qt::InputMethodQuery property) const override |
\reimp | |
Q_INVOKABLE QVariant | inputMethodQuery (Qt::InputMethodQuery query, QVariant argument) const |
Protected Member Functions | |
bool | event (QEvent *e) override |
\reimp | |
virtual void | keyPressEvent (QKeyEvent *ev) override |
The event ev is used to provide the following keyboard shortcuts: \table \header. | |
virtual void | mouseMoveEvent (QMouseEvent *ev) override |
\reimp | |
virtual void | mousePressEvent (QMouseEvent *ev) override |
\reimp | |
virtual void | mouseReleaseEvent (QMouseEvent *ev) override |
\reimp | |
virtual void | focusOutEvent (QFocusEvent *ev) override |
\reimp | |
virtual bool | focusNextPrevChild (bool next) override |
\reimp | |
virtual void | paintEvent (QPaintEvent *e) override |
\reimp | |
virtual void | doSetSource (const QUrl &name, QTextDocument::ResourceType type=QTextDocument::UnknownResource) |
Attempts to load the document at the given url with the specified type. | |
![]() | |
virtual bool | event (QEvent *e) override |
virtual void | timerEvent (QTimerEvent *e) override |
virtual void | keyPressEvent (QKeyEvent *e) override |
\reimp | |
virtual void | keyReleaseEvent (QKeyEvent *e) override |
\reimp | |
virtual void | resizeEvent (QResizeEvent *e) override |
\reimp | |
virtual void | paintEvent (QPaintEvent *e) override |
This event handler can be reimplemented in a subclass to receive paint events passed in event. | |
virtual void | mousePressEvent (QMouseEvent *e) override |
\reimp | |
virtual void | mouseMoveEvent (QMouseEvent *e) override |
\reimp | |
virtual void | mouseReleaseEvent (QMouseEvent *e) override |
\reimp | |
virtual void | mouseDoubleClickEvent (QMouseEvent *e) override |
\reimp | |
virtual bool | focusNextPrevChild (bool next) override |
\reimp | |
virtual void | contextMenuEvent (QContextMenuEvent *e) override |
Shows the standard context menu created with createStandardContextMenu(). | |
virtual void | focusInEvent (QFocusEvent *e) override |
\reimp | |
virtual void | focusOutEvent (QFocusEvent *e) override |
\reimp | |
virtual void | showEvent (QShowEvent *) override |
\reimp | |
virtual void | changeEvent (QEvent *e) override |
\reimp | |
virtual QMimeData * | createMimeDataFromSelection () const |
This function returns a new MIME data object to represent the contents of the text edit's current selection. | |
virtual bool | canInsertFromMimeData (const QMimeData *source) const |
This function returns true if the contents of the MIME data object, specified by source, can be decoded and inserted into the document. | |
virtual void | insertFromMimeData (const QMimeData *source) |
This function inserts the contents of the MIME data object, specified by source, into the text edit at the current cursor position. | |
virtual void | inputMethodEvent (QInputMethodEvent *) override |
\reimp | |
QTextEdit (QTextEditPrivate &dd, QWidget *parent) | |
virtual void | scrollContentsBy (int dx, int dy) override |
\reimp | |
virtual void | doSetTextCursor (const QTextCursor &cursor) |
void | zoomInF (float range) |
Properties | |
QUrl | source |
the name of the displayed document. | |
QTextDocument::ResourceType | sourceType |
the type of the displayed document | |
QStringList | searchPaths |
the search paths used by the text browser to find supporting content | |
bool | openExternalLinks |
bool | openLinks |
![]() | |
AutoFormatting | autoFormatting |
the enabled set of auto formatting features | |
bool | tabChangesFocus |
whether \uicontrol Tab changes focus or is accepted as input | |
QString | documentTitle |
the title of the document parsed from the text. | |
bool | undoRedoEnabled |
whether undo and redo are enabled. | |
LineWrapMode | lineWrapMode |
the line wrap mode | |
int | lineWrapColumnOrWidth |
the position (in pixels or columns depending on the wrap mode) where text will be wrapped | |
bool | readOnly |
whether the text edit is read-only | |
QString | html |
This property provides an HTML interface to the text of the text edit. | |
QString | plainText |
the text editor's contents as plain text. | |
bool | overwriteMode |
whether text entered by the user will overwrite existing text | |
qreal | tabStopDistance |
the tab stop distance in pixels | |
bool | acceptRichText |
whether the text edit accepts rich text insertions by the user | |
int | cursorWidth |
Qt::TextInteractionFlags | textInteractionFlags |
QTextDocument * | document |
the underlying document of the text editor. | |
QString | placeholderText |
the editor placeholder text | |
Additional Inherited Members | |
![]() | |
enum | LineWrapMode { NoWrap , WidgetWidth , FixedPixelWidth , FixedColumnWidth } |
\value NoWrap \value WidgetWidth \value FixedPixelWidth \value FixedColumnWidth More... | |
enum | AutoFormattingFlag { AutoNone = 0 , AutoBulletList = 0x00000001 , AutoAll = 0xffffffff } |
\value AutoNone Don't do any automatic formatting. More... | |
The QTextBrowser class provides a rich text browser with hypertext navigation.
\inmodule QtWidgets
This class extends QTextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents.
If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.
Definition at line 17 of file qtextbrowser.h.
Constructs an empty QTextBrowser with parent parent.
Definition at line 683 of file qtextbrowser.cpp.
References d.
|
virtual |
Definition at line 694 of file qtextbrowser.cpp.
This signal is emitted when the user clicks an anchor.
The URL referred to by the anchor is passed in link.
Note that the browser will automatically handle navigation to the location specified by link unless the openLinks property is set to false or you call setSource() in a slot connected. This mechanism is used to override the default navigation features of the browser.
|
virtualslot |
Changes the document displayed to the previous document in the list of documents built by navigating links.
Does nothing if there is no previous document.
Definition at line 906 of file qtextbrowser.cpp.
References backwardAvailable(), d, emit, forwardAvailable(), and historyChanged().
Referenced by keyPressEvent().
|
signal |
This signal is emitted when the availability of backward() changes.
available is false when the user is at home(); otherwise it is true.
Referenced by backward(), clearHistory(), doSetSource(), and forward().
int QTextBrowser::backwardHistoryCount | ( | ) | const |
Returns the number of locations backward in the history.
Definition at line 1254 of file qtextbrowser.cpp.
References d.
void QTextBrowser::clearHistory | ( | ) |
Clears the history of visited documents and disables the forward and backward navigation.
Definition at line 1184 of file qtextbrowser.cpp.
References backwardAvailable(), d, emit, forwardAvailable(), historyChanged(), and QTextBrowserPrivate::HistoryEntry::url.
|
protectedvirtual |
Attempts to load the document at the given url with the specified type.
setSource() calls doSetSource. In Qt 5, setSource(const QUrl &url) was virtual. In Qt 6, doSetSource() is virtual instead, so that it can be overridden in subclasses.
Definition at line 805 of file qtextbrowser.cpp.
References backwardAvailable(), d, QTextEdit::documentTitle, emit, forwardAvailable(), historyChanged(), QUrl::isValid(), and url.
Referenced by setSource().
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1308 of file qtextbrowser.cpp.
References e, and QTextEdit::event().
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1071 of file qtextbrowser.cpp.
References d, QTextEdit::focusNextPrevChild(), next, and url.
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1059 of file qtextbrowser.cpp.
References d, QTextEdit::focusOutEvent(), Qt::IBeamCursor, and Qt::TextEditable.
|
virtualslot |
Changes the document displayed to the next document in the list of documents built by navigating links.
Does nothing if there is no next document.
Definition at line 928 of file qtextbrowser.cpp.
References backwardAvailable(), d, emit, forwardAvailable(), and historyChanged().
Referenced by keyPressEvent().
|
signal |
This signal is emitted when the availability of forward() changes.
available is true after the user navigates backward() and false when the user navigates or goes forward().
Referenced by backward(), clearHistory(), doSetSource(), and forward().
int QTextBrowser::forwardHistoryCount | ( | ) | const |
Returns the number of locations forward in the history.
Definition at line 1243 of file qtextbrowser.cpp.
References d.
This signal is emitted when the user has selected but not activated an anchor in the document.
The URL referred to by the anchor is passed in link.
|
signal |
This signal is emitted when the history changes.
Referenced by backward(), clearHistory(), doSetSource(), and forward().
QString QTextBrowser::historyTitle | ( | int | i | ) | const |
Returns the documentTitle() of the HistoryItem.
\table \header
Definition at line 1231 of file qtextbrowser.cpp.
QUrl QTextBrowser::historyUrl | ( | int | i | ) | const |
Returns the url of the HistoryItem.
\table \header
Definition at line 1211 of file qtextbrowser.cpp.
|
virtualslot |
Changes the document displayed to be the first document from the history.
Definition at line 948 of file qtextbrowser.cpp.
References d, and setSource().
Referenced by keyPressEvent().
bool QTextBrowser::isBackwardAvailable | ( | ) | const |
Returns true
if the text browser can go backward in the document history using backward().
Definition at line 1156 of file qtextbrowser.cpp.
References d.
bool QTextBrowser::isForwardAvailable | ( | ) | const |
Returns true
if the text browser can go forward in the document history using forward().
Definition at line 1170 of file qtextbrowser.cpp.
References d.
The event ev is used to provide the following keyboard shortcuts: \table \header.
Reimplemented from QTextEdit.
Definition at line 964 of file qtextbrowser.cpp.
References QEvent::accept(), Qt::AltModifier, QTextCharFormat::anchorHref(), backward(), cursor, d, forward(), home(), QEvent::ignore(), QString::isEmpty(), QKeyEvent::key(), Qt::Key_Back, Qt::Key_Down, Qt::Key_Left, Qt::Key_Right, Qt::Key_Select, Qt::Key_Up, QTextEdit::keyPressEvent(), and QKeyEvent::modifiers().
This function is called when the document is loaded and for each image in the document.
The type indicates the type of resource to be loaded. An invalid QVariant is returned if the resource cannot be loaded.
The default implementation ignores type and tries to locate the resources by interpreting name as a file name. If it is not an absolute path it tries to find the file in the paths of the \l searchPaths property and in the same directory as the current source. On success, the result is a QVariant that stores a QByteArray with the contents of the file.
If you reimplement this function, you can return other QVariant types. The table below shows which variant types are supported depending on the resource type:
\table \header
Reimplemented from QTextEdit.
Definition at line 1129 of file qtextbrowser.cpp.
References d, fileName, and QIODeviceBase::ReadOnly.
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1035 of file qtextbrowser.cpp.
References e, and QTextEdit::mouseMoveEvent().
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1043 of file qtextbrowser.cpp.
References e, and QTextEdit::mousePressEvent().
|
overrideprotectedvirtual |
\reimp
Reimplemented from QTextEdit.
Definition at line 1051 of file qtextbrowser.cpp.
References e, and QTextEdit::mouseReleaseEvent().
bool QTextBrowser::openExternalLinks | ( | ) | const |
Definition at line 1271 of file qtextbrowser.cpp.
References d.
bool QTextBrowser::openLinks | ( | ) | const |
Definition at line 1295 of file qtextbrowser.cpp.
References d.
|
overrideprotectedvirtual |
|
virtualslot |
Reloads the current set source.
Definition at line 776 of file qtextbrowser.cpp.
References d, and setSource().
QStringList QTextBrowser::searchPaths | ( | ) | const |
Definition at line 761 of file qtextbrowser.cpp.
References d.
void QTextBrowser::setOpenExternalLinks | ( | bool | open | ) |
void QTextBrowser::setOpenLinks | ( | bool | open | ) |
void QTextBrowser::setSearchPaths | ( | const QStringList & | paths | ) |
Definition at line 767 of file qtextbrowser.cpp.
References d.
|
slot |
Attempts to load the document at the given url with the specified type.
If type is \l {QTextDocument::UnknownResource}{UnknownResource} (the default), the document type will be detected: that is, if the url ends with an extension of {.md},
{.mkd} or
{.markdown}, the document will be loaded via \l QTextDocument::setMarkdown(); otherwise it will be loaded via \l QTextDocument::setHtml(). This detection can be bypassed by specifying the type explicitly.
Definition at line 794 of file qtextbrowser.cpp.
References doSetSource(), and url.
Referenced by home(), main(), and reload().
QUrl QTextBrowser::source | ( | ) | const |
Definition at line 725 of file qtextbrowser.cpp.
References d.
This signal is emitted when the source has changed, src being the new source.
Source changes happen both programmatically when calling setSource(), forward(), backward() or home() or when the user clicks on links or presses the equivalent key sequences.
QTextDocument::ResourceType QTextBrowser::sourceType | ( | ) | const |
Definition at line 742 of file qtextbrowser.cpp.
References d, and QTextDocument::UnknownResource.
|
readwrite |
Specifies whether QTextBrowser should automatically open links to external sources using QDesktopServices::openUrl() instead of emitting the anchorClicked signal. Links are considered external if their scheme is neither file or qrc.
The default value is false.
Definition at line 90 of file qtextbrowser.h.
|
readwrite |
This property specifies whether QTextBrowser should automatically open links the user tries to activate by mouse or keyboard.
Regardless of the value of this property the anchorClicked signal is always emitted.
The default value is true.
Definition at line 90 of file qtextbrowser.h.
|
readwrite |
the search paths used by the text browser to find supporting content
QTextBrowser uses this list to locate images and documents.
By default, this property contains an empty string list.
Definition at line 90 of file qtextbrowser.h.
|
readwrite |
the name of the displayed document.
This is a an invalid url if no document is displayed or if the source is unknown.
When setting this property QTextBrowser tries to find a document with the specified name in the paths of the searchPaths property and directory of the current source, unless the value is an absolute file path. It also checks for optional anchors and scrolls the document accordingly
If the first tag in the document is {<qt type=detail>>}, the document is displayed as a popup rather than as new document in the browser window itself. Otherwise, the document is displayed normally in the text browser with the text set to the contents of the named document with \l QTextDocument::setHtml() or \l QTextDocument::setMarkdown(), depending on whether the filename ends with any of the known Markdown file extensions.
If you would like to avoid automatic type detection and specify the type explicitly, call setSource() rather than setting this property.
By default, this property contains an empty URL.
Definition at line 90 of file qtextbrowser.h.
|
read |
the type of the displayed document
This is QTextDocument::UnknownResource if no document is displayed or if the type of the source is unknown. Otherwise it holds the type that was detected, or the type that was specified when setSource() was called.
Definition at line 90 of file qtextbrowser.h.