QTextCursor Class
QTextCursor クラスは、QTextDocuments にアクセスして変更するための API を提供します。詳細...
ヘッダー | #include <QTextCursor> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
- 継承メンバを含む全メンバ一覧
- QTextCursor はRich Text Processing APIおよびImplicitly Shared Classes に含まれています。
注意:このクラスの関数はすべてリエントラントです。
パブリック型
enum | MoveMode { MoveAnchor, KeepAnchor } |
enum | MoveOperation { NoMove, Start, StartOfLine, StartOfBlock, StartOfWord, …, PreviousRow } |
enum | SelectionType { Document, BlockUnderCursor, LineUnderCursor, WordUnderCursor } |
パブリック関数
QTextCursor() | |
QTextCursor(QTextDocument *document) | |
QTextCursor(QTextFrame *frame) | |
QTextCursor(const QTextBlock &block) | |
QTextCursor(const QTextCursor &cursor) | |
~QTextCursor() | |
int | anchor() const |
bool | atBlockEnd() const |
bool | atBlockStart() const |
bool | atEnd() const |
bool | atStart() const |
void | beginEditBlock() |
QTextBlock | block() const |
QTextCharFormat | blockCharFormat() const |
QTextBlockFormat | blockFormat() const |
int | blockNumber() const |
QTextCharFormat | charFormat() const |
void | clearSelection() |
int | columnNumber() const |
QTextList * | createList(const QTextListFormat &format) |
QTextList * | createList(QTextListFormat::Style style) |
QTextFrame * | currentFrame() const |
QTextList * | currentList() const |
QTextTable * | currentTable() const |
void | deleteChar() |
void | deletePreviousChar() |
QTextDocument * | document() const |
void | endEditBlock() |
bool | hasComplexSelection() const |
bool | hasSelection() const |
void | insertBlock() |
void | insertBlock(const QTextBlockFormat &format) |
void | insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat) |
void | insertFragment(const QTextDocumentFragment &fragment) |
QTextFrame * | insertFrame(const QTextFrameFormat &format) |
void | insertHtml(const QString &html) |
void | insertImage(const QTextImageFormat &format) |
void | insertImage(const QString &name) |
void | insertImage(const QImage &image, const QString &name = QString()) |
void | insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment) |
QTextList * | insertList(const QTextListFormat &format) |
QTextList * | insertList(QTextListFormat::Style style) |
(since 6.4) void | insertMarkdown(const QString &markdown, QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) |
QTextTable * | insertTable(int rows, int columns, const QTextTableFormat &format) |
QTextTable * | insertTable(int rows, int columns) |
void | insertText(const QString &text) |
void | insertText(const QString &text, const QTextCharFormat &format) |
bool | isCopyOf(const QTextCursor &other) const |
bool | isNull() const |
void | joinPreviousEditBlock() |
bool | keepPositionOnInsert() const |
void | mergeBlockCharFormat(const QTextCharFormat &modifier) |
void | mergeBlockFormat(const QTextBlockFormat &modifier) |
void | mergeCharFormat(const QTextCharFormat &modifier) |
bool | movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = MoveAnchor, int n = 1) |
int | position() const |
int | positionInBlock() const |
void | removeSelectedText() |
void | select(QTextCursor::SelectionType selection) |
void | selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const |
QString | selectedText() const |
QTextDocumentFragment | selection() const |
int | selectionEnd() const |
int | selectionStart() const |
void | setBlockCharFormat(const QTextCharFormat &format) |
void | setBlockFormat(const QTextBlockFormat &format) |
void | setCharFormat(const QTextCharFormat &format) |
void | setKeepPositionOnInsert(bool b) |
void | setPosition(int pos, QTextCursor::MoveMode m = MoveAnchor) |
void | setVerticalMovementX(int x) |
void | setVisualNavigation(bool b) |
void | swap(QTextCursor &other) |
int | verticalMovementX() const |
bool | visualNavigation() const |
bool | operator!=(const QTextCursor &other) const |
bool | operator<(const QTextCursor &other) const |
bool | operator<=(const QTextCursor &other) const |
QTextCursor & | operator=(const QTextCursor &cursor) |
bool | operator==(const QTextCursor &other) const |
bool | operator>(const QTextCursor &other) const |
bool | operator>=(const QTextCursor &other) const |
詳細説明
テキスト・カーソルは、テキストエディタにおけるカーソルの動作を模倣したプログラミング・インターフェースを介して、テキスト・ドキュメントの内容や基本的な構造にアクセスしたり変更したりするために使用されるオブジェクトです。QTextCursor は、QTextDocument 内のカーソルの位置と、カーソルが行った選択に関する情報を保持します。
QTextCursorは、テキストエディタにおけるテキストカーソルの動作をモデルにしており、ユーザーインターフェイスを通じて標準的なアクションを実行するためのプログラム的な手段を提供します。ドキュメントは1つの文字列と考えることができます。カーソルの現在のposition ()は、常に文字列の連続する2文字の間か、文字列の最初の文字の前か、最後の文字の後のどちらかになります。ドキュメントには、テキスト以外に表、リスト、画像、その他のオブジェクトが含まれることもありますが、開発者の視点からは、ドキュメントは1つの長い文字列として扱うことができます。その文字列の一部は、特定のブロック(段落など)内、あるいは表のセル内、リストの項目内、その他の構造要素内にあると考えることができます。現在の文字」とは、文書中のカーソルposition() の直前の文字を意味します。同様に、「現在のブロック」とは、カーソルposition() を含むブロックのことです。
QTextCursor にもanchor() の位置があります。anchor() とposition() の間にあるテキストが選択範囲です。anchor() ==position() の場合は選択ではありません。
カーソルの位置は、setPosition() およびmovePosition() を使用してプログラムで変更できます。選択については、selectionStart()、selectionEnd()、hasSelection()、clearSelection()、removeSelectedText() を参照。
position() がブロックの先頭の場合、atBlockStart() はtrue
を返し、ブロックの末尾の場合、atBlockEnd() は true を返す。現在の文字の書式はcharFormat() で返され、現在のブロックの書式はblockFormat() で返される。
setCharFormat(),mergeCharFormat(),setBlockFormat(),mergeBlockFormat() 関数を使用して、現在のテキスト文書に書式を適用することができます。set'関数はカーソルの現在の文字またはブロックの書式を置換し、'merge'関数は与えられた書式プロパティをカーソルの現在の書式に追加する。カーソルに選択範囲がある場合、指定された書式は現在の選択範囲に適用される。ブロックの一部だけが選択されている場合、ブロック書式はブロック全体に適用されることに注意してください。現在の文字位置のテキストは、createList ()を使ってリストにすることができる。
削除はdeleteChar()、deletePreviousChar()、removeSelectedText() を使って行う。
テキスト文字列はinsertText() 関数で文書に挿入できます。ブロック (新しい段落を表す) はinsertBlock() で挿入できます。
既存のテキストの断片はinsertFragment() で挿入できますが、さまざまな形式のテキストを挿入したい場合は、insertText() を使って文字形式を指定したほうが簡単です。
カーソルを使って、さまざまな種類の上位構造を文書に挿入することもできます:
- リストは、箇条書きや記号で装飾されたブロック要素の順序付きシーケンスです。これらはinsertList() で指定した書式で挿入されます。
- 表はinsertTable ()関数で挿入され、任意の書式を指定することができます。表にはセルの配列が含まれ、カーソルを使って移動することができます。
- インライン画像はinsertImage() で挿入する。使用する画像は、画像形式または名前で指定できます。
- フレームは、insertFrame ()を指定されたフォーマットで呼び出すことで挿入される。
beginEditBlock() およびendEditBlock() を使用すると、アクションをグループ化できる(つまり、アンドゥ/リドゥを 1 つのアクションとして扱える)。
カーソルの移動は、有効なカーソル位置に限定される。ラテン文字では、テキスト中の連続する2文字の間、最初の文字の前、最後の文字の後です。他の文字システムでは、カーソル移動は「クラスター」(デーヴァナーガリー語の音節や、基本文字と発音区別記号など)に制限されるものもあります。movePosition() やdeleteChar() のような関数は、カーソルの移動をこれらの有効な位置に制限します。
リッチテキスト処理も参照のこと 。
メンバ型ドキュメント
enum QTextCursor::MoveMode
定数 | 値 | 説明 |
---|---|---|
QTextCursor::MoveAnchor | 0 | アンカーをカーソル自身と同じ位置に移動する。 |
QTextCursor::KeepAnchor | 1 | アンカーを現在の位置に維持します。 |
anchor() の位置を維持したままposition() を移動すると、その間のテキストが選択されます。
enum QTextCursor::MoveOperation
定数 | 値 | 説明 |
---|---|---|
QTextCursor::NoMove | 0 | カーソルを現在位置に保つ |
QTextCursor::Start | 1 | ドキュメントの先頭に移動 |
QTextCursor::StartOfLine | 3 | 現在の行の先頭に移動する。 |
QTextCursor::StartOfBlock | 4 | 現在のブロックの先頭に移動する。 |
QTextCursor::StartOfWord | 5 | 現在の単語の先頭に移動する。 |
QTextCursor::PreviousBlock | 6 | 前のブロックの先頭に移動する。 |
QTextCursor::PreviousCharacter | 7 | 前の文字に移動する。 |
QTextCursor::PreviousWord | 8 | 前の単語の先頭に移動 |
QTextCursor::Up | 2 | 1行上に移動 |
QTextCursor::Left | 9 | 左に1文字移動 |
QTextCursor::WordLeft | 10 | 左に1文字移動 |
QTextCursor::End | 11 | 文書の末尾に移動する。 |
QTextCursor::EndOfLine | 13 | 現在の行の最後に移動する。 |
QTextCursor::EndOfWord | 14 | 現在の単語の末尾に移動 |
QTextCursor::EndOfBlock | 15 | 現在のブロックの末尾に移動 |
QTextCursor::NextBlock | 16 | 次のブロックの先頭に移動 |
QTextCursor::NextCharacter | 17 | 次の文字に移動する。 |
QTextCursor::NextWord | 18 | 次の単語に移動する。 |
QTextCursor::Down | 12 | 1行下に移動 |
QTextCursor::Right | 19 | 右に1文字移動 |
QTextCursor::WordRight | 20 | 右に1単語移動 |
QTextCursor::NextCell | 21 | 現在の表内の次の表のセルの先頭に移動する。現在のセルが行の最後のセルの場合、カーソルは次の行の最初のセルに移動する。 |
QTextCursor::PreviousCell | 22 | 現在の表内の前の表のセルの先頭に移動する。現在のセルが行の最初のセルの場合、カーソルは前の行の最後のセルに移動する。 |
QTextCursor::NextRow | 23 | 現在の表の次の行の最初の新しいセルに移動する。 |
QTextCursor::PreviousRow | 24 | 現在の表の前の行の最後のセルに移動する。 |
movePosition()も参照のこと 。
enum QTextCursor::SelectionType
この列挙型は、select() 関数で適用できる選択の種類を記述します。
定数 | 値 | 説明 |
---|---|---|
QTextCursor::Document | 3 | 文書全体を選択します。 |
QTextCursor::BlockUnderCursor | 2 | カーソル下のテキストブロックを選択します。 |
QTextCursor::LineUnderCursor | 1 | カーソル下のテキスト行を選択します。 |
QTextCursor::WordUnderCursor | 0 | カーソル下の単語を選択する。カーソルが選択可能な文字列内にない場合、テキストは選択されない。 |
メンバ関数ドキュメント
QTextCursor::QTextCursor()
NULLカーソルを構築する。
[explicit]
QTextCursor::QTextCursor(QTextDocument *document)
document の先頭を指すカーソルを構築する。
[explicit]
QTextCursor::QTextCursor(QTextFrame *frame)
frame の先頭を指すカーソルを構築する。
[explicit]
QTextCursor::QTextCursor(const QTextBlock &block)
block の先頭を指すカーソルを構築する。
QTextCursor::QTextCursor(const QTextCursor &cursor)
cursor のコピーである新しいカーソルを構築する。
[noexcept]
QTextCursor::~QTextCursor()
QTextCursor を破壊する。
int QTextCursor::anchor() const
これは、position() と同じである。ただし、選択範囲がある場合は、position() が選択範囲の一方の端を示し、anchor() がもう一方の端を示す。カーソル位置と同様、アンカー位置は文字と文字の間になります。
position(),setPosition(),movePosition(),selectionStart(),selectionEnd()も参照のこと 。
bool QTextCursor::atBlockEnd() const
カーソルがブロックの末尾にある場合はtrue
を返し、そうでない場合はfalse
を返す。
atBlockStart() およびatEnd()も参照のこと 。
bool QTextCursor::atBlockStart() const
カーソルがブロックの先頭にある場合はtrue
を返し、そうでない場合はfalse
を返す。
atBlockEnd() およびatStart()も参照のこと 。
bool QTextCursor::atEnd() const
カーソルがドキュメントの末尾にある場合はtrue
を返し、そうでない場合はfalse
を返す。
atStart() およびatBlockEnd()も参照のこと 。
bool QTextCursor::atStart() const
カーソルがドキュメントの先頭にある場合はtrue
を返し、そうでない場合はfalse
を返す。
atBlockStart() およびatEnd()も参照のこと 。
void QTextCursor::beginEditBlock()
ドキュメントに対する編集操作のブロックの開始を示し、アンドゥ/リドゥの観点からは単一の操作として表示されます。
例えば
QTextCursor cursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); textDocument->undo();
undo()をコールすると、両方の挿入が取り消され、"World "と "Hello "の両方が削除されます。
beginEditBlockとendEditBlock 。一番上のペアが、元に戻す/やり直す操作の範囲を決定する。
endEditBlock()も参照のこと 。
QTextBlock QTextCursor::block() const
カーソルを含むブロックを返す。
QTextCharFormat QTextCursor::blockCharFormat() const
カーソルがあるブロックのブロック文字書式を返す。
ブロック文字書式は、空のブロックの先頭にテキストを挿入するときに使用する書式です。
setBlockCharFormat()も参照 。
QTextBlockFormat QTextCursor::blockFormat() const
カーソルがあるブロックのブロックフォーマットを返す。
setBlockFormat() およびcharFormat()も参照 。
int QTextCursor::blockNumber() const
カーソルが存在するブロックの番号を返し、カーソルが無効な場合は0を返す。
この関数は、テーブルやフレームのような複雑なオブジェクトを持たない文書でのみ意味を持つことに注意してください。
QTextCharFormat QTextCursor::charFormat() const
カーソルposition()の直前の文字の書式を返す。カーソルが空でないテキストブロックの先頭にある場合は、カーソルの直後の文字の書式が返されます。
setCharFormat()、insertText()、blockFormat()も参照 。
void QTextCursor::clearSelection()
アンカーをカーソル位置にセットすることにより、現在の選択範囲をクリアする。
選択範囲のテキストは削除されないことに注意。
removeSelectedText() およびhasSelection()も参照 。
int QTextCursor::columnNumber() const
カーソルが含まれる行内の位置を返す。
これは折り返し行からの相対的な列番号であり、ブロック(つまり段落)からの相対的な列番号ではないことに注意してください。
おそらく、代わりにpositionInBlock() を呼び出したいでしょう。
positionInBlock()も参照 。
QTextList *QTextCursor::createList(const QTextListFormat &format)
与えられたformat で新しいリストを作成し、それを返します。
insertList() およびcurrentList()も参照 。
QTextList *QTextCursor::createList(QTextListFormat::Style style)
これはオーバーロードされた関数です。
与えられたstyle で新しいリストを作成し、カーソルの現在の段落を最初のリスト項目として返します。
使用されるスタイルはQTextListFormat::Style enumで定義されます。
insertList() およびcurrentList()も参照してください 。
QTextFrame *QTextCursor::currentFrame() const
現在のフレームへのポインタを返す。カーソルが無効な場合はnullptr
を返す。
insertFrame()も参照 。
QTextList *QTextCursor::currentList() const
カーソルposition() がリストの一部であるブロック内にある場合、現在のリストを返す。そうでない場合はnullptr
を返す。
insertList() およびcreateList()も参照 。
QTextTable *QTextCursor::currentTable() const
カーソルposition() がテーブルの一部であるブロック内にある場合、現在のテーブルへのポインタを返す。そうでない場合はnullptr
を返す。
insertTable()も参照のこと 。
void QTextCursor::deleteChar()
選択テキストがない場合は、現在のカーソル位置の文字を削除し、そうでない場合は選択テキストを削除する。
deletePreviousChar()、hasSelection()、clearSelection()も参照 。
void QTextCursor::deletePreviousChar()
選択テキストがない場合は、現在のカーソル位置より前の文字を削除し、そうでない場合は選択テキストを削除する。
deleteChar()、hasSelection()、clearSelection()も参照 。
QTextDocument *QTextCursor::document() const
このカーソルが関連付けられているドキュメントを返します。
void QTextCursor::endEditBlock()
文書に対する編集操作のブロックの終了を示し、 undo/redoの観点からは単一の操作として表示されます。
beginEditBlock()も参照 。
bool QTextCursor::hasComplexSelection() const
カーソルがselectionStart() からselectionEnd() までの単純な範囲でない選択範囲を含んでいる場合、true
を返し、そうでない場合はfalse
を返す。
複雑な選択範囲とは、テーブルの少なくとも2つのセルにまたがる選択範囲のことで、その範囲はselectedTableCells() で指定します。
bool QTextCursor::hasSelection() const
カーソルが選択範囲を含んでいる場合はtrue
を返し、そうでない場合はfalse
を返す。
void QTextCursor::insertBlock()
カーソルposition() に、現在のblockFormat() とcharFormat() で新しい空のブロックを挿入する。
setBlockFormat()も参照 。
void QTextCursor::insertBlock(const QTextBlockFormat &format)
これはオーバーロードされた関数である。
カーソルposition() に、ブロック書式format および現在のcharFormat() を char ブロック書式として、新しい空のブロックを挿入する。
setBlockFormat()も参照 。
void QTextCursor::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat)
これはオーバーロードされた関数である。
ブロック書式format とcharFormat をブロック文字書式として、カーソルposition() に新しい空のブロックを挿入する。
setBlockFormat()も参照 。
void QTextCursor::insertFragment(const QTextDocumentFragment &fragment)
テキス トfragment を現在のposition() に挿入する。
QTextFrame *QTextCursor::insertFrame(const QTextFrameFormat &format)
現在のカーソルposition() に、与えられたformat を持つフレームを挿入し、カーソルposition() をフレーム内に移動し、フレームを返す。
カーソルが選択範囲を保持している場合、選択範囲全体がフレーム内に移動します。
hasSelection()も参照してください 。
void QTextCursor::insertHtml(const QString &html)
テキストhtml を現在のposition() に挿入する。テキストはHTMLとして解釈される。
注意: スタイル・シートと共にこの関数を使用する場合、スタイル・シートは文書内の現在のブロックにのみ適用されます。文書全体にスタイルシートを適用するには、代わりにQTextDocument::setDefaultStyleSheet() を使用してください。
void QTextCursor::insertImage(const QTextImageFormat &format)
format で定義された画像を、現在のposition() に挿入する。
void QTextCursor::insertImage(const QString &name)
これはオーバーロードされた関数です。
与えられたname の画像を、現在のposition() に挿入するための便利なメソッドです。
QImage img; textDocument->addResource(QTextDocument::ImageResource, QUrl("myimage"), img); cursor.insertImage("myimage");
void QTextCursor::insertImage(const QImage &image, const QString &name = QString())
これはオーバーロードされた関数である。
与えられたimage を、現在のposition() にオプションのname とともに挿入するための便利な関数です。
void QTextCursor::insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment)
これはオーバーロードされた関数である。
与えられたformat で定義された画像を、指定されたalignment でカーソルの現在位置に挿入する。
position()も参照 。
QTextList *QTextCursor::insertList(const QTextListFormat &format)
現在の位置に新しいブロックを挿入し、与えられたformat で新しく作成されたリストの最初のリスト項目にします。作成されたリストを返します。
currentList()、createList() およびinsertBlock()も参照 。
QTextList *QTextCursor::insertList(QTextListFormat::Style style)
これはオーバーロードされた関数です。
現在の位置に新しいブロックを挿入し、与えられたstyle で新しく作成されたリストの最初のリスト項目にします。作成されたリストを返します。
currentList()、createList()、insertBlock()も参照してください 。
[since 6.4]
void QTextCursor::insertMarkdown(const QString &markdown, QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub)
現在のposition() に、指定された Markdownfeatures でmarkdown テキストを挿入します。デフォルトは GitHub 方言です。
この関数は Qt 6.4 で導入されました。
QTextTable *QTextCursor::insertTable(int rows, int columns, const QTextTableFormat &format)
指定されたformat に、指定された数のrows とcolumns を持つ新しい表を作成し、文書内の現在のカーソルposition() に挿入し、表オブジェクトを返す。カーソルは最初のセルの先頭に移動します。
テーブルには、少なくとも1つの行と1つの列が必要です。
currentTable()も参照 。
QTextTable *QTextCursor::insertTable(int rows, int columns)
これはオーバーロードされた関数である。
与えられた数のrows とcolumns を持つ新しい表を作成し、それをドキュメントの現在のカーソルposition() に挿入し、表オブジェクトを返します。カーソルは最初のセルの先頭に移動します。
テーブルには、少なくとも1つの行と1つの列が必要です。
currentTable()も参照 。
void QTextCursor::insertText(const QString &text)
現在の文字書式で、現在の位置にtext を挿入する。
選択範囲がある場合、その選択範囲は削除され、例えばtext で置き換えられる:
cursor.clearSelection(); cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); cursor.insertText("Hello World");
これは、既存の選択範囲をすべてクリアし、カーソル位置の単語 (position() から先) を選択し、選択範囲を "Hello World" というフレーズで置き換えます。
挿入されたテキスト内の ASCII 改行文字 (\n) は、insertBlock() 呼び出しに対応する unicode ブロック区切り文字に変換されます。
charFormat() およびhasSelection()も参照のこと 。
void QTextCursor::insertText(const QString &text, const QTextCharFormat &format)
これはオーバーロードされた関数である。
与えられたformat でtext を現在の位置に挿入する。
bool QTextCursor::isCopyOf(const QTextCursor &other) const
このカーソルとother がお互いのコピーである場合、つまりどちらかがもう一方のコピーとして作成され、その後どちらも移動していない場合、true
を返します。これは等価よりもはるかに厳格である。
operator=() およびoperator==()も参照のこと 。
bool QTextCursor::isNull() const
カーソルがNULLの場合はtrue
、それ以外の場合はfalse
を返します。NULLカーソルはデフォルトのコンストラクタによって作成されます。
void QTextCursor::joinPreviousEditBlock()
beginEditBlock()と同様に、アンドゥ/リドゥのための単一の操作として表示されるべき編集操作のブロックの開始を示す。しかし、beginEditBlock()とは異なり、新しいブロックを開始するのではなく、endEditBlock()を呼び出す前の操作を取り消すため、次の操作は前に作成された編集ブロックの一部となります。
例えば
QTextCursor cursor(textDocument); cursor.beginEditBlock(); cursor.insertText("Hello"); cursor.insertText("World"); cursor.endEditBlock(); // ... cursor.joinPreviousEditBlock(); cursor.insertText("Hey"); cursor.endEditBlock(); textDocument->undo();
undo()を呼び出すと、3つの挿入がすべて取り消されます。
beginEditBlock() およびendEditBlock()も参照の こと。
bool QTextCursor::keepPositionOnInsert() const
カーソルの位置にテキストが挿入されたときに、カーソルの現在位置を保持するかどうかを返します。
デフォルトは false です;
setKeepPositionOnInsert()も参照 。
void QTextCursor::mergeBlockCharFormat(const QTextCharFormat &modifier)
カ レ ン ト ブ ロ ッ ク (または選択範囲に含まれるすべてのブ ロ ッ ク ) の文字ブ ロ ッ ク 書式を、modifier で指定 さ れたブ ロ ッ ク 書式で変更 し ます。
setBlockCharFormat()も参照 。
void QTextCursor::mergeBlockFormat(const QTextBlockFormat &modifier)
カ レ ン ト ブ ロ ッ ク (または選択範囲に含まれるすべてのブ ロ ッ ク ) のブ ロ ッ ク 書式を、modifier で指定 さ れてい る ブ ロ ッ ク 書式で変更 し ます。
setBlockFormat() およびblockFormat()も参照 。
void QTextCursor::mergeCharFormat(const QTextCharFormat &modifier)
カーソルの現在の文字書式を formatmodifier で記述されたプロパティとマージする。カーソルに選択範囲がある場合、この関数は、modifier で設定されたすべてのプロパティを、選択範囲の一部であるすべての文字書式に適用する。
hasSelection() およびsetCharFormat()も参照のこと 。
bool QTextCursor::movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = MoveAnchor, int n = 1)
指定されたmode を使用して、指定されたoperation n を何度も実行してカーソルを移動し、すべての操作が正常に完了した場合はtrue
を返し、そうでない場合はfalse
を返す。
例えば、この関数を繰り返し使用して次の単語の末尾をシークすると、最終的に文書の末尾に到達した時点で失敗する。
デフォルトでは、移動操作は1回だけ行われる (n = 1)。
mode がKeepAnchor
の場合、カーソルは移動したテキストを選択する。これは、ユーザーがShiftキーを押しながらカーソルキーでカーソルを移動させたときと同じ効果である。
setVisualNavigation()も参照のこと 。
int QTextCursor::position() const
ドキュメント内のカーソルの絶対位置を返す。カーソルは文字と文字の間に配置される。
注意: この場合の "文字 "は、QChar オブジェクトの文字列、すなわち16ビットのUnicode文字を指し、位置はこの文字列のインデックスとみなされます。サロゲートペアや言語的合字、発音区分のように、1つの書記素が複数のUnicode文字で表現されることがあるからです。
setPosition(),movePosition(),anchor(),positionInBlock()も参照のこと 。
int QTextCursor::positionInBlock() const
ブロック内のカーソルの相対位置を返す。カーソルは文字と文字の間に配置される。
これはposition() - block().position()
と等価である。
注意: この場合の "文字 "は、QChar オブジェクトの文字列、つまり16ビットのUnicode文字を指し、位置はこの文字列のインデックスとみなされます。サロゲートペアや言語的合字、発音区分のように、1つの書記素が複数のUnicode文字で表現されることがあるからです。
position()も参照 。
void QTextCursor::removeSelectedText()
選択範囲があればその内容が削除され、なければ何もしない。
hasSelection()も参照 。
void QTextCursor::select(QTextCursor::SelectionType selection)
与えられたselection に従っ て文書内のテ キ ス ト を選択 し ます。
void QTextCursor::selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const
選択範囲が表セルにまたがる場合、firstRow には選択範囲の最初の行の番号が、firstColumn には選択範囲の最初の列の番号が、numRows とnumColumns には選択範囲の行と列の番号が入ります。選択範囲がテーブルセルにまたがっていない場合、結果は無害ですが未定義です。
QString QTextCursor::selectedText() const
現在の選択範囲のテキストを返します(空でもかまいません)。これはテキストのみを返し、リッチテキストの書式情報は返しません。ドキュメントの断片 (つまりフォーマットされたリッチテキスト) が必要な場合は、代わりにselection() を使用してください。
注: エディタから取得した選択範囲が改行にまたがる場合、テキストには改行\n
文字の代わりに Unicode U+2029 段落区切り文字が含まれます。これらの文字を改行に置き換えるにはQString::replace() を使います。
QTextDocumentFragment QTextCursor::selection() const
現在の選択範囲 (空でもよい) を、すべての書式情報とともに返します。選択されたテキスト (すなわちプレーンテキスト) だけが欲しい場合は、代わりにselectedText() を使用します。
注意: QTextDocumentFragment::toPlainText() とは異なり、selectedText() にはQChar::ParagraphSeparator のような特殊な unicode 文字が含まれることがあります。
QTextDocumentFragment::toPlainText()も参照してください 。
int QTextCursor::selectionEnd() const
選択範囲の終わりを返すか、カーソルに選択範囲がない場合はposition() を返す。
selectionStart()、position()、anchor()も参照 。
int QTextCursor::selectionStart() const
選択範囲の開始位置を返すか、カーソルに選択範囲がない場合はposition() を返す。
selectionEnd()、position()、anchor()も参照 。
void QTextCursor::setBlockCharFormat(const QTextCharFormat &format)
カ レ ン ト ブ ロ ッ ク (または選択範囲に含まれるすべてのブ ロ ッ ク ) のブ ロ ッ ク文字書式をformat に設定する。
blockCharFormat()も参照 。
void QTextCursor::setBlockFormat(const QTextBlockFormat &format)
カ レ ン ト ブ ロ ッ ク (または選択範囲に含まれるすべてのブ ロ ッ ク ) のブ ロ ッ ク 書式をformat に設定。
blockFormat() およびmergeBlockFormat()も参照 。
void QTextCursor::setCharFormat(const QTextCharFormat &format)
カーソルの現在の文字フォーマットを指定されたformat に設定する。カーソルに選択範囲がある場合、与えられたformat が現在の選択範囲に適用される。
charFormat()、hasSelection()、mergeCharFormat()も参照 。
void QTextCursor::setKeepPositionOnInsert(bool b)
カーソルの現在位置にテキストが挿入されたとき、カーソルが現在位置を保持するかどうかを定義する。
b がtrueの場合、テキストがカーソルの位置に挿入されたとき、カーソルは現在の位置を維持する。b が偽の場合、カーソルは挿入されたテキストと共に移動する。
デフォルトはfalseである。
カーソルの現在位置より前にテキストが挿入されると、カーソルは常に移動し、カーソルの現在位置より後にテキストが挿入されると、カーソルは常にその位置を維持することに注意してください。
keepPositionOnInsert()も参照 。
void QTextCursor::setPosition(int pos, QTextCursor::MoveMode m = MoveAnchor)
m で指定されたMoveMode
を使って、pos で指定されたドキュメント内の絶対位置にカーソルを移動する。カーソルは文字と文字の間に配置される。
注意: この場合の "文字 "は、QChar オブジェクトの文字列、すなわち16ビットUnicode文字を指し、pos はこの文字列へのインデックスとみなされる。サロゲートペアや言語的合字、ダイアクリティクスの場合など、1つの書記素が複数のUnicode文字で表現されることがあるからです。文書をナビゲートするためのより一般的なアプローチとしては、movePosition() を使って、テキスト中の実際の書記素境界を尊重します。
position(),movePosition(),anchor()も参照 。
void QTextCursor::setVerticalMovementX(int x)
垂直カーソル移動の視覚的 x 位置をx に設定する。
カーソルが水平方向に移動すると、垂直方向の移動 x 位置は自動的にクリアされ、カーソルが垂直方向に移動すると変更されない。このメカニズムにより、カーソルはプロポーショナルフォントで視覚的に直線上を上下に移動し、短い行を緩やかに「ジャンプ」することができる。
値 -1 は、あらかじめ定義された x 位置がないことを示す。次にカーソルが上下に動いたときに自動的に設定される。
verticalMovementX()も参照のこと 。
void QTextCursor::setVisualNavigation(bool b)
ビジュアル・ナビゲーションをb に設定する。
ビジュアル・ナビゲーションとは、隠れたテキストの段落をスキップすることです。デフォルトは false です。
visualNavigation() およびmovePosition()も参照 。
[noexcept]
void QTextCursor::swap(QTextCursor &other)
このテキスト・カーソルのインスタンスをother と交換する。この操作は非常に高速で、失敗することはない。
int QTextCursor::verticalMovementX() const
垂直方向のカーソル移動の視覚的な x 位置を返す。
値 -1 は、定義済みの x 位置がないことを示す。この値は、カーソルが次に上下に移動したときに自動的に設定されます。
setVerticalMovementX()も参照 。
bool QTextCursor::visualNavigation() const
カーソルがビジュアル・ナビゲーションを行う場合はtrue
を返し、そうでない場合はfalse
を返す。
ビジュアル・ナビゲーションとは、隠されたテキストの段落をスキップすることです。デフォルトは false です。
setVisualNavigation() およびmovePosition()も参照 。
bool QTextCursor::operator!=(const QTextCursor &other) const
other カーソルがこのカーソルと文書内の異なる位置にある場合はtrue
を返し、そうでない場合はfalse
を返す。
bool QTextCursor::operator<(const QTextCursor &other) const
other カーソルがこのカーソルよりドキュメント内で後に位置している場合はtrue
を返し、そうでない場合はfalse
を返す。
bool QTextCursor::operator<=(const QTextCursor &other) const
other カーソルがこのカーソルと同じドキュメントの後か同じ位置にある場合はtrue
を返し、そうでない場合は false を返す。
QTextCursor &QTextCursor::operator=(const QTextCursor &cursor)
cursor のコピーを作成し、このQTextCursor に代入する。QTextCursor は暗黙の共有クラスであることに注意。
bool QTextCursor::operator==(const QTextCursor &other) const
other カーソルがこのカーソルと同じドキュメントの位置にある場合はtrue
を返し、そうでない場合はfalse
を返す。
bool QTextCursor::operator>(const QTextCursor &other) const
other カーソルがこのカーソルよりもドキュメント内で前に位置している場合はtrue
を返し、そうでない場合はfalse
を返す。
bool QTextCursor::operator>=(const QTextCursor &other) const
other カーソルがこのカーソルとドキュメント内で同じ位置にある場合はtrue
を返し、そうでない場合は false を返す。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.