📄 qtextcursor.cpp
字号:
\sa atBlockEnd(), atStart()*/bool QTextCursor::atBlockStart() const{ if (!d || !d->priv) return false; return d->position == d->block().position();}/*! Returns true if the cursor is at the end of a block; otherwise returns false. \sa atBlockStart(), atEnd()*/bool QTextCursor::atBlockEnd() const{ if (!d || !d->priv) return false; return d->position == d->block().position() + d->block().length() - 1;}/*! Returns true if the cursor is at the start of the document; otherwise returns false. \sa atBlockStart(), atEnd()*/bool QTextCursor::atStart() const{ if (!d || !d->priv) return false; return d->position == 0;}/*! Returns true if the cursor is at the end of the document; otherwise returns false. \sa atStart(), atBlockEnd()*/bool QTextCursor::atEnd() const{ if (!d || !d->priv) return false; return d->position == d->priv->length() - 1;}/*! Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat(). \sa setBlockFormat()*/void QTextCursor::insertBlock(){ insertBlock(blockFormat());}/*! \overload Inserts a new empty block at the cursor position() with block format \a format and the current charFormat() as block char format. \sa setBlockFormat()*/void QTextCursor::insertBlock(const QTextBlockFormat &format){ QTextCharFormat charFmt = charFormat(); charFmt.clearProperty(QTextFormat::ObjectType); insertBlock(format, charFmt);}/*! \overload Inserts a new empty block at the cursor position() with block format \a format and \a charFormat as block char format. \sa setBlockFormat()*/void QTextCursor::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat){ if (!d || !d->priv) return; d->priv->beginEditBlock(); d->remove(); d->insertBlock(format, charFormat); d->priv->endEditBlock();}/*! Inserts a new block at the current position and makes it the first list item of a newly created list with the given \a format. Returns the created list. \sa currentList() createList() insertBlock() */QTextList *QTextCursor::insertList(const QTextListFormat &format){ insertBlock(); return createList(format);}/*! \overload Inserts a new block at the current position and makes it the first list item of a newly created list with the given \a style. Returns the created list. \sa currentList(), createList(), insertBlock() */QTextList *QTextCursor::insertList(QTextListFormat::Style style){ insertBlock(); return createList(style);}/*! Creates and returns a new list with the given \a format, and makes the current paragraph the cursor is in the first list item. \sa insertList() currentList() */QTextList *QTextCursor::createList(const QTextListFormat &format){ if (!d || !d->priv) return 0; QTextList *list = static_cast<QTextList *>(d->priv->createObject(format)); QTextBlockFormat modifier; modifier.setObjectIndex(list->objectIndex()); mergeBlockFormat(modifier); return list;}/*! \overload Creates and returns a new list with the given \a style, making the cursor's current paragraph the first list item. The style to be used is defined by the QTextListFormat::Style enum. \sa insertList() currentList() */QTextList *QTextCursor::createList(QTextListFormat::Style style){ QTextListFormat fmt; fmt.setStyle(style); return createList(fmt);}/*! Returns the current list if the cursor position() is inside a block that is part of a list; otherwise returns 0. \sa insertList() createList() */QTextList *QTextCursor::currentList() const{ if (!d || !d->priv) return 0; QTextBlockFormat b = blockFormat(); QTextObject *o = d->priv->objectForFormat(b); return qobject_cast<QTextList *>(o);}/*! \fn QTextTable *QTextCursor::insertTable(int rows, int columns) \overload Creates a new table with the given number of \a rows and \a columns, inserts it at the current cursor position() in the document, and returns the table object. The cursor is moved to the beginning of the first cell. There must be at least one row and one column in the table. \sa currentTable() */QTextTable *QTextCursor::insertTable(int rows, int cols){ return insertTable(rows, cols, QTextTableFormat());}/*! \fn QTextTable *QTextCursor::insertTable(int rows, int columns, const QTextTableFormat &format) Creates a new table with the given number of \a rows and \a columns in the specified \a format, inserts it at the current cursor position() in the document, and returns the table object. The cursor is moved to the beginning of the first cell. There must be at least one row and one column in the table. \sa currentTable()*/QTextTable *QTextCursor::insertTable(int rows, int cols, const QTextTableFormat &format){ if(!d || !d->priv || rows == 0 || cols == 0) return 0; int pos = d->position; QTextTable *t = QTextTablePrivate::createTable(d->priv, d->position, rows, cols, format); d->setPosition(pos+1); // ##### what should we do if we have a selection? d->anchor = d->position; d->adjusted_anchor = d->anchor; return t;}/*! Returns a pointer to the current table if the cursor position() is inside a block that is part of a table; otherwise returns 0. \sa insertTable()*/QTextTable *QTextCursor::currentTable() const{ if(!d || !d->priv) return 0; QTextFrame *frame = d->priv->frameAt(d->position); while (frame) { QTextTable *table = qobject_cast<QTextTable *>(frame); if (table) return table; frame = frame->parentFrame(); } return 0;}/*! Inserts a frame with the given \a format at the current cursor position(), moves the cursor position() inside the frame, and returns the frame. If the cursor holds a selection, the whole selection is moved inside the frame. \sa hasSelection()*/QTextFrame *QTextCursor::insertFrame(const QTextFrameFormat &format){ if (!d || !d->priv) return 0; return d->priv->insertFrame(selectionStart(), selectionEnd(), format);}/*! Returns a pointer to the current frame. Returns 0 if the cursor is invalid. \sa insertFrame()*/QTextFrame *QTextCursor::currentFrame() const{ if(!d || !d->priv) return 0; return d->priv->frameAt(d->position);}/*! Inserts the text \a fragment at the current position().*/void QTextCursor::insertFragment(const QTextDocumentFragment &fragment){ if (!d || !d->priv || fragment.isEmpty()) return; d->priv->beginEditBlock(); d->remove(); fragment.d->insert(*this); d->priv->endEditBlock();}/*! Inserts the image defined by \a format at the current position().*/void QTextCursor::insertImage(const QTextImageFormat &format){ insertText(QString(QChar::ObjectReplacementCharacter), format);}/*! \overload Convenience method for inserting the image with the given \a name at the current position().*/void QTextCursor::insertImage(const QString &name){ QTextImageFormat format; format.setName(name); insertImage(format);}/*! \fn bool QTextCursor::operator!=(const QTextCursor &other) const Returns true if the \a other cursor is at a different position in the document as this cursor; otherwise returns false.*/bool QTextCursor::operator!=(const QTextCursor &rhs) const{ return !operator==(rhs);}/*! \fn bool QTextCursor::operator<(const QTextCursor &other) const Returns true if the \a other cursor is positioned later in the document than this cursor; otherwise returns false.*/bool QTextCursor::operator<(const QTextCursor &rhs) const{ if (!d) return !!rhs.d; if (!rhs.d) return false; Q_ASSERT_X(d->priv == rhs.d->priv, "QTextCursor::operator<", "cannot compare cursors attached to different documents"); return d->position < rhs.d->position;}/*! \fn bool QTextCursor::operator<=(const QTextCursor &other) const Returns true if the \a other cursor is positioned later or at the same position in the document as this cursor; otherwise returns false.*/bool QTextCursor::operator<=(const QTextCursor &rhs) const{ if (!d) return true; if (!rhs.d) return false; Q_ASSERT_X(d->priv == rhs.d->priv, "QTextCursor::operator<=", "cannot compare cursors attached to different documents"); return d->position <= rhs.d->position;}/*! \fn bool QTextCursor::operator==(const QTextCursor &other) const Returns true if the \a other cursor is at the same position in the document as this cursor; otherwise returns false.*/bool QTextCursor::operator==(const QTextCursor &rhs) const{ if (!d) return !rhs.d; if (!rhs.d) return false; return d->position == rhs.d->position && d->priv == rhs.d->priv;}/*! \fn bool QTextCursor::operator>=(const QTextCursor &other) const Returns true if the \a other cursor is positioned earlier or at the same position in the document as this cursor; otherwise returns false.*/bool QTextCursor::operator>=(const QTextCursor &rhs) const{ if (!d) return false; if (!rhs.d) return true; Q_ASSERT_X(d->priv == rhs.d->priv, "QTextCursor::operator>=", "cannot compare cursors attached to different documents"); return d->position >= rhs.d->position;}/*! \fn bool QTextCursor::operator>(const QTextCursor &other) const Returns true if the \a other cursor is positioned earlier in the document than this cursor; otherwise returns false.*/bool QTextCursor::operator>(const QTextCursor &rhs) const{ if (!d) return false; if (!rhs.d) return true; Q_ASSERT_X(d->priv == rhs.d->priv, "QTextCursor::operator>", "cannot compare cursors attached to different documents"); return d->position > rhs.d->position;}/*! Indicates the start of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view. For example: \code QTextCursor cursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); textDocument->undo(); \endcode The call to undo() will cause both insertions to be undone, causing both "World" and "Hello" to be removed. \sa endEditBlock() */void QTextCursor::beginEditBlock(){ if (!d || !d->priv) return; d->priv->beginEditBlock();}/*! Like beginEditBlock() indicates the start of a block of editing operations that should appear as a single operation for undo/redo. However unlike beginEditBlock() it does not start a new block but reverses the previous call to endEditBlock() and therefore makes following operations part of the previous edit block created. For example: \code QTextCursor cursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); ... cursor.joinPreviousEditBlock(); cursor.insertText("Hey"); cursor.endEditBlock(); textDocument->undo(); \endcode The call to undo() will cause all three insertions to be undone. \sa beginEditBlock(), endEditBlock() */void QTextCursor::joinPreviousEditBlock(){ if (!d || !d->priv) return; d->priv->joinPreviousEditBlock();}/*! Indicates the end of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view. \sa beginEditBlock() */void QTextCursor::endEditBlock(){ if (!d || !d->priv) return; d->priv->endEditBlock();}/*! Returns true if this cursor and \a other are copies of each other, i.e. one of them was created as a copy of the other and neither has moved since. This is much stricter than equality. \sa operator=() operator==()*/bool QTextCursor::isCopyOf(const QTextCursor &other) const{ return d == other.d;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -