📄 qtextcursor.cpp
字号:
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();}/*! \since 4.2 Inserts the text \a html at the current position(). The text is interpreted as HTML. \note When using this function with a style sheet, the style sheet will only apply to the current block in the document. In order to apply a style sheet throughout a document, use QTextDocument::setDefaultStyleSheet() instead.*/void QTextCursor::insertHtml(const QString &html){ if (!d || !d->priv) return; QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(html, d->priv->document()); insertFragment(fragment);}/*! \overload \since 4.2 Inserts the image defined by the given \a format at the cursor's current position with the specified \a alignment. \sa position()*/void QTextCursor::insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment){ if (!d || !d->priv) return; QTextFrameFormat ffmt; ffmt.setPosition(alignment); QTextObject *obj = d->priv->createObject(ffmt); QTextImageFormat fmt = format; fmt.setObjectIndex(obj->objectIndex()); d->priv->beginEditBlock(); d->remove(); const int idx = d->priv->formatCollection()->indexForFormat(fmt); d->priv->insert(d->position, QString(QChar(QChar::ObjectReplacementCharacter)), idx); 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(). \code QImage img = ... textDocument->addResource(QTextDocument::ImageResource, QUrl("myimage"), img); cursor.insertImage("myimage"); \endcode*/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;}/*! \since 4.2 Returns the number of the block the cursor is in. Note that this function only makes sense in documents without complex objects such as tables or frames.*/int QTextCursor::blockNumber() const{ if (!d || !d->priv) return 0; // ### naive implementation for now QTextBlock currentBlock = d->block(); if (!currentBlock.isValid()) return 0; int count = 0; for (QTextBlock block = d->priv->blocksBegin(); block.isValid() && block != currentBlock; block = block.next(), ++count) { } return count;}/*! \since 4.2 Returns the position of the cursor within its containing line.*/int QTextCursor::columnNumber() const{ if (!d || !d->priv) return 0; QTextBlock block = d->block(); if (!block.isValid()) return 0; const QTextLayout *layout = block.layout(); Q_ASSERT(layout); // can't happen const int relativePos = d->position - block.position(); if (layout->lineCount() == 0) return relativePos; QTextLine line = layout->lineForTextPosition(relativePos); if (!line.isValid()) return 0; return relativePos - line.t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -