📄 qtextdocument.cpp
字号:
/*! Sets the document's meta information of the type specified by \a info to the given \a string. \sa metaInformation()*/void QTextDocument::setMetaInformation(MetaInformation info, const QString &string){ if (info != DocumentTitle) return; Q_D(QTextDocument); d->title = string;}/*! Returns the plain text contained in the document. If you want formatting information use a QTextCursor instead. \sa toHtml()*/QString QTextDocument::toPlainText() const{ Q_D(const QTextDocument); QString txt = d->plainText(); txt.replace(QTextBeginningOfFrame, QLatin1Char('\n')); txt.replace(QTextEndOfFrame, QLatin1Char('\n')); txt.replace(QChar::ParagraphSeparator, QLatin1Char('\n')); txt.replace(QChar::LineSeparator, QLatin1Char('\n')); txt.replace(QChar::Nbsp, QLatin1Char(' ')); return txt;}/*! Replaces the entire contents of the document with the given plain \a text. \sa setHtml()*/void QTextDocument::setPlainText(const QString &text){ Q_D(QTextDocument); bool previousState = d->isUndoRedoEnabled(); d->enableUndoRedo(false); d->clear(); QTextCursor(this).insertText(text); d->enableUndoRedo(previousState);}/*! Replaces the entire contents of the document with the given HTML-formatted text in the \a html string. The HTML formatting is respected as much as possible; for example, "<b>bold</b> text" will produce text where the first word has a font weight that gives it a bold appearance: "\bold{bold} text". \note It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created and passed to setHtml(). \sa setPlainText(), {Supported HTML Subset}*/void QTextDocument::setHtml(const QString &html){ Q_D(QTextDocument); bool previousState = d->isUndoRedoEnabled(); d->enableUndoRedo(false); d->clear(); QTextHtmlImporter(this, html, QTextHtmlImporter::ImportToDocument).import(); d->enableUndoRedo(previousState);}/*! \enum QTextDocument::FindFlag This enum describes the options available to QTextDocument's find function. The options can be OR-red together from the following list: \value FindBackward Search backwards instead of forwards. \value FindCaseSensitively By default find works case insensitive. Specifying this option changes the behaviour to a case sensitive find operation. \value FindWholeWords Makes find match only complete words.*//*! \enum QTextDocument::MetaInformation This enum describes the different types of meta information that can be added to a document. \value DocumentTitle The title of the document. \sa metaInformation(), setMetaInformation()*//*! \fn QTextCursor QTextDocument::find(const QString &subString, int position, FindFlags options) const \overload Finds the next occurrence of the string, \a subString, in the document. The search starts at the given \a position, and proceeds forwards through the document unless specified otherwise in the search options. The \a options control the type of search performed. Returns a cursor with the match selected if \a subString was found; otherwise returns a null cursor. If the \a position is 0 (the default) the search begins from the beginning of the document; otherwise it begins at the specified position.*/QTextCursor QTextDocument::find(const QString &subString, int from, FindFlags options) const{ QRegExp expr(subString); expr.setPatternSyntax(QRegExp::FixedString); expr.setCaseSensitivity((options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); return find(expr, from, options);}/*! \fn QTextCursor QTextDocument::find(const QString &subString, const QTextCursor &cursor, FindFlags options) const Finds the next occurrence of the string, \a subString, in the document. The search starts at the position of the given \a cursor, and proceeds forwards through the document unless specified otherwise in the search options. The \a options control the type of search performed. Returns a cursor with the match selected if \a subString was found; otherwise returns a null cursor. If the given \a cursor has a selection, the search begins after the selection; otherwise it begins at the cursor's position. By default the search is case-sensitive, and can match text anywhere in the document.*/QTextCursor QTextDocument::find(const QString &subString, const QTextCursor &from, FindFlags options) const{ int pos = 0; if (!from.isNull()) { if (options & QTextDocument::FindBackward) pos = from.selectionStart(); else pos = from.selectionEnd(); } QRegExp expr(subString); expr.setPatternSyntax(QRegExp::FixedString); expr.setCaseSensitivity((options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); return find(expr, pos, options);}static bool findInBlock(const QTextBlock &block, const QRegExp &expression, int offset, QTextDocument::FindFlags options, QTextCursor &cursor){ const QRegExp expr(expression); QString text = block.text(); text.replace(QChar::Nbsp, QLatin1Char(' ')); int idx = -1; while (offset >=0 && offset <= text.length()) { idx = (options & QTextDocument::FindBackward) ? expr.lastIndexIn(text, offset) : expr.indexIn(text, offset); if (idx == -1) return false; if (options & QTextDocument::FindWholeWords) { const int start = idx; const int end = start + expr.matchedLength(); if ((start != 0 && text.at(start - 1).isLetterOrNumber()) || (end != text.length() && text.at(end).isLetterOrNumber())) { //if this is not a whole word, continue the search in the string offset = (options & QTextDocument::FindBackward) ? idx-1 : end+1; idx = -1; continue; } } //we have a hit, return the cursor for that. break; } if (idx == -1) return false; cursor = QTextCursor(block.docHandle(), block.position() + idx); cursor.setPosition(cursor.position() + expr.matchedLength(), QTextCursor::KeepAnchor); return true;}/*! \fn QTextCursor QTextDocument::find(const QRegExp & expr, int position, FindFlags options) const \overload Finds the next occurrence, matching the regular expression, \a expr, in the document. The search starts at the given \a position, and proceeds forwards through the document unless specified otherwise in the search options. The \a options control the type of search performed. The FindCaseSensitively option is ignored for this overload, use QRegExp::caseSensitivity instead. Returns a cursor with the match selected if a match was found; otherwise returns a null cursor. If the \a position is 0 (the default) the search begins from the beginning of the document; otherwise it begins at the specified position.*/QTextCursor QTextDocument::find(const QRegExp & expr, int from, FindFlags options) const{ Q_D(const QTextDocument); if (expr.isEmpty()) return QTextCursor(); int pos = from; //the cursor is positioned between characters, so for a backward search //do not include the character given in the position. if (options & FindBackward) { --pos ; if(pos < 0) return QTextCursor(); } QTextCursor cursor; QTextBlock block = d->blocksFind(pos); if (!(options & FindBackward)) { int blockOffset = qMax(0, pos - block.position()); while (block.isValid()) { if (findInBlock(block, expr, blockOffset, options, cursor)) return cursor; blockOffset = 0; block = block.next(); } } else { int blockOffset = pos - block.position(); while (block.isValid()) { if (findInBlock(block, expr, blockOffset, options, cursor)) return cursor; block = block.previous(); blockOffset = block.length() - 1; } } return QTextCursor();}/*! \fn QTextCursor QTextDocument::find(const QRegExp &expr, const QTextCursor &cursor, FindFlags options) const Finds the next occurrence, matching the regular expression, \a expr, in the document. The search starts at the position of the given \a cursor, and proceeds forwards through the document unless specified otherwise in the search options. The \a options control the type of search performed. The FindCaseSensitively option is ignored for this overload, use QRegExp::caseSensitivity instead. Returns a cursor with the match selected if a match was found; otherwise returns a null cursor. If the given \a cursor has a selection, the search begins after the selection; otherwise it begins at the cursor's position. By default the search is case-sensitive, and can match text anywhere in the document.*/QTextCursor QTextDocument::find(const QRegExp &expr, const QTextCursor &from, FindFlags options) const{ int pos = 0; if (!from.isNull()) { if (options & QTextDocument::FindBackward) pos = from.selectionStart(); else pos = from.selectionEnd(); } return find(expr, pos, options);}/*! \fn QTextObject *QTextDocument::createObject(const QTextFormat &format) Creates and returns a new document object (a QTextObject), based on the given \a format. QTextObjects will always get created through this method, so you must reimplement it if you use custom text objects inside your document.*/QTextObject *QTextDocument::createObject(const QTextFormat &f){ QTextObject *obj = 0; if (f.isListFormat()) obj = new QTextList(this); else if (f.isTableFormat()) obj = new QTextTable(this); else if (f.isFrameFormat()) obj = new QTextFrame(this); return obj;}/*! \internal Returns the frame that contains the text cursor position \a pos.*/QTextFrame *QTextDocument::frameAt(int pos) const{ Q_D(const QTextDocument); return d->frameAt(pos);}/*! Returns the document's root frame.*/QTextFrame *QTextDocument::rootFrame() const{ Q_D(const QTextDocument); return d->rootFrame();}/*! Returns the text object associated with the given \a objectIndex.*/QTextObject *QTextDocument::object(int objectIndex) const{ Q_D(const QTextDocument); return d->objectForIndex(objectIndex);}/*! Returns the text object associated with the format \a f.*/QTextObject *QTextDocument::objectForFormat(const QTextFormat &f) const{ Q_D(const QTextDocument); return d->objectForFormat(f);}/*! Returns the text block that contains the \a{pos}-th character.*/QTextBlock QTextDocument::findBlock(int pos) const{ Q_D(const QTextDocument); return QTextBlock(docHandle(), d->blockMap().findNode(pos));}/*! Returns the document's first text block.*/QTextBlock QTextDocument::begin() const{ Q_D(const QTextDocument); return QTextBlock(docHandle(), d->blockMap().begin().n);}/*! This function returns a block to test for the end of the document while iterating over it. \quotefromfile snippets/textdocumentendsnippet.cpp \skipto for \printuntil cout The block returned is invalid and represents the block after the last block in the document.*/QTextBlock QTextDocument::end() const{ return QTextBlock(docHandle(), 0);}/*! \property QTextDocument::pageSize \brief the page size that should be used for laying out the document \sa modificationChanged()*/void QTextDocument::setPageSize(const QSizeF &size){ Q_D(QTextDocument); d->pageSize = size; if (d->lout) d->lout->documentChanged(0, 0, d->length());}QSizeF QTextDocument::pageSize() const{ Q_D(const QTextDocument); return d->pageSize;}/*! returns the number of pages in this document.*/int QTextDocument::pageCount() const{ return documentLayout()->pageCount();}/*! Sets the default \a font to use in the document layout.*/void QTextDocument::setDefaultFont(const QFont &font){ Q_D(QTextDocument); d->setDefaultFont(font); if (d->lout) d->lout->documentChanged(0, 0, d->length());}/*! Returns the default font to be used in the document layout.*/QFont QTextDocument::defaultFont() const{ Q_D(const QTextDocument); return d->defaultFont();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -