QTextCursor Class

QTextCursor 클래스는 QTextDocuments에 액세스하고 수정하는 API를 제공합니다. 더 보기...

Header: #include <QTextCursor>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

참고: 이 클래스의 모든 함수는 재진입됩니다.

공용 유형

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는 텍스트 편집기에서 텍스트 커서가 동작하는 방식을 모델링하여 사용자 인터페이스를 통해 표준 동작을 수행하는 프로그래밍 수단을 제공합니다. 문서는 하나의 문자열로 생각할 수 있습니다. 그러면 커서의 현재 position()는 항상 문자열의 연속된 두 문자 사이에 있거나 문자열의 첫 번째 문자 또는 마지막 문자 뒤에 위치합니다. 문서에는 텍스트 외에도 표, 목록, 이미지 및 기타 개체가 포함될 수 있지만 개발자의 관점에서는 문서를 하나의 긴 문자열로 취급할 수 있습니다. 이 문자열의 일부는 특정 블록(예: 단락), 표의 셀, 목록의 항목 또는 기타 구조적 요소 내에 있는 것으로 간주할 수 있습니다. "현재 문자"는 문서에서 커서 position() 바로 앞에 있는 문자를 의미합니다. 마찬가지로 "현재 블록"은 커서 position()가 포함된 블록을 의미합니다.

QTextCursor에는 anchor() 위치도 있습니다. anchor ()와 position() 사이에 있는 텍스트가 선택 영역입니다. anchor () == position()인 경우 선택 영역이 없습니다.

커서 위치는 setPosition() 및 movePosition()를 사용하여 프로그래밍 방식으로 변경할 수 있으며, 후자는 텍스트를 선택하는 데에도 사용할 수 있습니다. 선택 항목은 selectionStart(), selectionEnd(), hasSelection(), clearSelection() 및 removeSelectedText()를 참조하세요.

position()가 블록의 시작 부분에 있는 경우 atBlockStart()은 true 을 반환하고, 블록의 끝에 있는 경우 atBlockEnd()은 참을 반환합니다. 현재 문자의 서식은 charFormat()가 반환하고, 현재 블록의 서식은 blockFormat()가 반환합니다.

setCharFormat(), mergeCharFormat(), setBlockFormat() 및 mergeBlockFormat() 함수를 사용하여 현재 텍스트 문서에 서식을 적용할 수 있습니다. 'set' 함수는 커서의 현재 문자 또는 블록 서식을 바꾸고, 'merge' 함수는 커서의 현재 서식에 지정된 서식 속성을 추가합니다. 커서에 선택 영역이 있는 경우 지정된 포맷이 현재 선택 영역에 적용됩니다. 블록의 일부만 선택된 경우 블록 서식은 전체 블록에 적용됩니다. 현재 문자 위치의 텍스트는 createList()를 사용하여 목록으로 바꿀 수 있습니다.

삭제는 deleteChar(), deletePreviousChar() 및 removeSelectedText()를 사용하여 수행할 수 있습니다.

insertText() 함수를 사용하여 문서에 텍스트 문자열을 삽입할 수 있고, insertBlock()을 사용하여 블록(새 단락을 나타냄)을 삽입할 수 있습니다.

기존 텍스트 조각은 insertFragment()로 삽입할 수 있지만, 다양한 형식의 텍스트 조각을 삽입하려면 일반적으로 insertText()를 사용하여 문자 형식을 지정하는 것이 더 쉽습니다.

커서를 사용하여 문서에 다양한 유형의 상위 구조를 삽입할 수도 있습니다:

  • 목록은 글머리 기호나 기호로 장식된 블록 요소의 정렬된 시퀀스입니다. 목록은 insertList()를 사용하여 지정된 형식으로 삽입됩니다.
  • 표는 insertTable() 함수를 사용하여 삽입되며 선택적 형식을 지정할 수 있습니다. 여기에는 커서를 사용하여 이동할 수 있는 셀 배열이 포함됩니다.
  • 인라인 이미지는 insertImage()로 삽입됩니다. 사용할 이미지는 이미지 형식 또는 이름으로 지정할 수 있습니다.
  • 프레임은 지정된 형식으로 insertFrame()를 호출하여 삽입합니다.

beginEditBlock() 및 endEditBlock()을 사용하여 작업을 그룹화할 수 있습니다(즉, 실행 취소/다시 실행을 위한 단일 작업으로 처리).

커서 이동은 유효한 커서 위치로 제한됩니다. 라틴 문자에서는 텍스트의 연속된 두 문자 사이, 첫 번째 문자 앞 또는 마지막 문자 뒤입니다. 일부 다른 문자 체계에서는 커서 이동이 "클러스터"(예: 데바나가리의 한 음절 또는 기본 문자와 분음 부호)로 제한됩니다. movePosition () 및 deleteChar()와 같은 함수는 커서 이동을 이러한 유효한 위치로 제한합니다.

서식 있는텍스트 처리도참조하세요 .

멤버 유형 문서

enum QTextCursor::MoveMode

상수설명
QTextCursor::MoveAnchor0앵커를 커서 자체와 같은 위치로 이동합니다.
QTextCursor::KeepAnchor1앵커를 현재 위치에 유지합니다.

anchor()를 그대로 유지하고 position()을 이동하면 그 사이의 텍스트가 선택됩니다.

enum QTextCursor::MoveOperation

상수설명
QTextCursor::NoMove0커서를 현재 위치에 유지합니다.
QTextCursor::Start1문서의 시작 부분으로 이동합니다.
QTextCursor::StartOfLine3현재 줄의 시작 부분으로 이동합니다.
QTextCursor::StartOfBlock4현재 블록의 시작 부분으로 이동합니다.
QTextCursor::StartOfWord5현재 단어의 시작 부분으로 이동합니다.
QTextCursor::PreviousBlock6이전 블록의 시작 부분으로 이동합니다.
QTextCursor::PreviousCharacter7이전 문자로 이동합니다.
QTextCursor::PreviousWord8이전 단어의 시작 부분으로 이동합니다.
QTextCursor::Up2한 줄 위로 이동합니다.
QTextCursor::Left9한 글자 왼쪽으로 이동합니다.
QTextCursor::WordLeft10한 단어 왼쪽으로 이동합니다.
QTextCursor::End11문서의 끝으로 이동합니다.
QTextCursor::EndOfLine13현재 줄의 끝으로 이동합니다.
QTextCursor::EndOfWord14현재 단어의 끝으로 이동합니다.
QTextCursor::EndOfBlock15현재 블록의 끝으로 이동합니다.
QTextCursor::NextBlock16다음 블록의 시작 부분으로 이동합니다.
QTextCursor::NextCharacter17다음 문자로 이동합니다.
QTextCursor::NextWord18다음 단어로 이동합니다.
QTextCursor::Down12한 줄 아래로 이동합니다.
QTextCursor::Right19한 글자 오른쪽으로 이동합니다.
QTextCursor::WordRight20한 단어 오른쪽으로 이동합니다.
QTextCursor::NextCell21현재 표 안의 다음 표 셀의 시작 부분으로 이동합니다. 현재 셀이 행의 마지막 셀인 경우 커서가 다음 행의 첫 번째 셀로 이동합니다.
QTextCursor::PreviousCell22현재 표 안의 이전 표 셀의 시작 부분으로 이동합니다. 현재 셀이 행의 첫 번째 셀인 경우 커서가 이전 행의 마지막 셀로 이동합니다.
QTextCursor::NextRow23현재 표에서 다음 행의 첫 번째 새 셀로 이동합니다.
QTextCursor::PreviousRow24현재 표에서 이전 행의 마지막 셀로 이동합니다.

movePosition()도 참조하세요 .

enum QTextCursor::SelectionType

이 열거형은 select() 함수에 적용할 수 있는 선택 유형에 대해 설명합니다.

Constant설명
QTextCursor::Document3전체 문서를 선택합니다.
QTextCursor::BlockUnderCursor2커서 아래의 텍스트 블록을 선택합니다.
QTextCursor::LineUnderCursor1커서 아래의 텍스트 줄을 선택합니다.
QTextCursor::WordUnderCursor0커서 아래의 단어를 선택합니다. 커서가 선택 가능한 문자 문자열 내에 위치하지 않으면 텍스트가 선택되지 않습니다.

멤버 함수 문서

QTextCursor::QTextCursor()

널 커서를 생성합니다.

[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()은 선택 영역의 한쪽 끝을 표시하고 anchor()는 다른 쪽 끝을 표시하는 경우를 제외하고 position()와 동일하게 앵커 위치를 반환합니다. 커서 위치와 마찬가지로 앵커 위치는 문자 사이입니다.

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"가 모두 제거됩니다.

startEditBlock과 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 열거형에 의해 정의됩니다.

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()

실행 취소/다시 실행 관점에서 단일 작업으로 표시되어야 하는 문서에 대한 편집 작업 블록의 끝을 나타냅니다.

beginEditBlock()도 참조하세요 .

bool QTextCursor::hasComplexSelection() const

커서에 selectionStart() ~ selectionEnd() 범위가 아닌 선택 영역이 포함된 경우 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

복잡한 선택은 표에서 두 개 이상의 셀에 걸쳐 있는 선택이며, 범위는 selectedTableCells()로 지정합니다.

bool QTextCursor::hasSelection() const

커서에 선택 항목이 포함되어 있으면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

void QTextCursor::insertBlock()

현재 blockFormat() 및 charFormat()와 함께 커서 position()에 새 빈 블록을 삽입합니다.

setBlockFormat()도 참조하세요 .

void QTextCursor::insertBlock(const QTextBlockFormat &format)

이 함수는 오버로드된 함수입니다.

커서 position()에 블록 형식 format 및 현재 charFormat()를 블록 문자 형식으로 새 빈 블록을 삽입합니다.

setBlockFormat()도 참조하세요 .

void QTextCursor::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat)

이 함수는 오버로드된 함수입니다.

커서 position()에 블록 형식 format 및 블록 문자 형식 charFormat 으로 새 빈 블록을 삽입합니다.

setBlockFormat()도 참조하세요 .

void QTextCursor::insertFragment(const QTextDocumentFragment &fragment)

현재 position()에 fragment 텍스트를 삽입합니다.

QTextFrame *QTextCursor::insertFrame(const QTextFrameFormat &format)

현재 커서 position()에 주어진 format 으로 프레임을 삽입하고 커서 position()를 프레임 내부로 이동한 다음 프레임을 반환합니다.

커서가 선택 영역을 가리키고 있으면 전체 선택 영역이 프레임 내부로 이동합니다.

hasSelection()도 참조하세요 .

void QTextCursor::insertHtml(const QString &html)

현재 position()에 html 텍스트를 삽입합니다. 텍스트는 HTML로 해석됩니다.

참고: 이 함수를 스타일시트와 함께 사용하는 경우 스타일시트는 문서의 현재 블록에만 적용됩니다. 문서 전체에 스타일 시트를 적용하려면 QTextDocument::setDefaultStyleSheet()를 대신 사용합니다.

void QTextCursor::insertImage(const QTextImageFormat &format)

현재 position()에 format 에 정의된 이미지를 삽입합니다.

void QTextCursor::insertImage(const QString &name)

이 함수는 과부하된 함수입니다.

현재 position()에 주어진 name 으로 이미지를 삽입하는 편리한 방법입니다.

QImage img;
textDocument->addResource(QTextDocument::ImageResource, QUrl("myimage"), img);
cursor.insertImage("myimage");

void QTextCursor::insertImage(const QImage &image, const QString &name = QString())

이 함수는 과부하된 함수입니다.

현재 position()에 선택 사항인 name 과 함께 주어진 image 을 삽입하는 편의 함수입니다.

void QTextCursor::insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment)

이 함수는 오버로드된 함수입니다.

지정된 alignment 을 사용하여 커서의 현재 위치에 지정된 format 으로 정의된 이미지를 삽입합니다.

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()에 지정된 마크다운 features 과 함께 markdown 텍스트를 삽입합니다. 기본값은 GitHub 방언입니다.

이 함수는 Qt 6.4에 도입되었습니다.

QTextTable *QTextCursor::insertTable(int rows, int columns, const QTextTableFormat &format)

지정된 format 에 지정된 수 rowscolumns 의 새 테이블을 만들고 문서의 현재 커서 position()에 삽입한 다음 테이블 객체를 반환합니다. 커서가 첫 번째 셀의 시작 부분으로 이동합니다.

표에는 적어도 하나의 행과 하나의 열이 있어야 합니다.

currentTable()도 참조하세요 .

QTextTable *QTextCursor::insertTable(int rows, int columns)

이 함수는 오버로드된 함수입니다.

주어진 개수 rowscolumns 의 새 표를 생성하고 문서의 현재 커서 position()에 삽입한 다음 표 개체를 반환합니다. 커서가 첫 번째 셀의 시작 부분으로 이동합니다.

표에는 적어도 하나의 행과 하나의 열이 있어야 합니다.

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() 호출에 해당합니다.

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

커서가 널이면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다. 널 커서는 기본 생성자에 의해 생성됩니다.

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()를 호출하면 세 개의 삽입이 모두 실행 취소됩니다.

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)

커서의 현재 문자 형식을 modifier 에 설명된 속성과 병합합니다. 커서에 선택 영역이 있는 경우 이 함수는 modifier 에 설정된 모든 속성을 선택 영역의 일부인 모든 문자 형식에 적용합니다.

hasSelection() 및 setCharFormat()도 참조하세요 .

bool QTextCursor::movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = MoveAnchor, int n = 1)

지정된 mode 을 사용하여 지정된 operation n 번 수행하여 커서를 이동하고 모든 작업이 성공적으로 완료되면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

예를 들어, 이 함수를 반복적으로 사용하여 다음 단어의 끝을 찾는 경우 문서의 끝에 도달하면 결국 실패합니다.

기본적으로 이동 작업은 한 번 수행됩니다(n = 1).

modeKeepAnchor 인 경우 커서가 이동하는 텍스트를 선택합니다. 이는 사용자가 Shift 키를 누른 상태에서 커서 키를 사용하여 커서를 이동할 때와 동일한 효과를 얻을 수 있습니다.

setVisualNavigation()도 참조하세요 .

int QTextCursor::position() const

문서 내에서 커서의 절대 위치를 반환합니다. 커서는 문자 사이에 위치합니다.

참고: 이 경우 "문자"는 QChar 객체의 문자열, 즉 16비트 유니코드 문자를 의미하며 위치는 이 문자열에 대한 인덱스로 간주됩니다. 대리 쌍, 언어 결합 또는 분음 부호 등의 경우처럼 하나의 문자가 여러 개의 유니코드 문자로 표현될 수 있으므로 이는 문자 체계의 개별 문자와 반드시 일치하지는 않습니다.

setPosition(), movePosition(), anchor() 및 positionInBlock()도 참조하세요 .

int QTextCursor::positionInBlock() const

블록 내에서 커서의 상대적 위치를 반환합니다. 커서는 문자 사이에 위치합니다.

이는 position() - block().position() 에 해당합니다.

참고: 이 경우 "문자"는 QChar 객체의 문자열, 즉 16비트 유니코드 문자를 의미하며 위치는 이 문자열에 대한 인덱스로 간주됩니다. 대리 쌍, 언어 결합 또는 분음 부호 등의 경우처럼 하나의 문자가 여러 개의 유니코드 문자로 표현될 수 있으므로 이는 문자 체계의 개별 문자와 반드시 일치하지는 않습니다.

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 에는 선택 영역의 첫 번째 열 번호가, numRowsnumColumns 에는 선택 영역의 행과 열 번호가 채워집니다. 선택 영역이 테이블 셀에 걸쳐 있지 않은 경우 결과는 무해하지만 정의되지 않습니다.

QString QTextCursor::selectedText() const

현재 선택 영역의 텍스트를 반환합니다(비어 있을 수 있음). 이 함수는 서식 있는 텍스트 정보 없이 텍스트만 반환합니다. 문서 조각(서식 있는 서식 있는 텍스트)을 원한다면 selection()를 대신 사용하세요.

참고: 편집기에서 가져온 선택 내용이 줄 바꿈에 걸쳐 있는 경우 텍스트에 줄 바꿈 대신 유니코드 U+2029 단락 구분 문자 \n 문자가 포함됩니다. 이러한 문자를 개행으로 바꾸려면 QString::replace()를 사용합니다.

QTextDocumentFragment QTextCursor::selection() const

현재 선택 영역(비어 있을 수 있음)을 모든 서식 정보와 함께 반환합니다. 선택한 텍스트(예: 일반 텍스트)만 반환하려면 selectedText()를 대신 사용합니다.

참고: QTextDocumentFragment::toPlainText()와 달리 selectedText()에는 QChar::ParagraphSeparator 와 같은 특수 유니코드 문자가 포함될 수 있습니다.

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 이 참이면 커서가 위치한 위치에 텍스트가 삽입될 때 커서가 현재 위치를 유지합니다. b 이 거짓이면 커서가 삽입된 텍스트와 함께 이동합니다.

기본값은 false입니다.

커서의 현재 위치 앞에 텍스트가 삽입되면 커서가 항상 이동하고, 커서의 현재 위치 뒤에 텍스트가 삽입되면 커서가 항상 그 위치를 유지한다는 점에 유의하세요.

keepPositionOnInsert()도 참조하세요 .

void QTextCursor::setPosition(int pos, QTextCursor::MoveMode m = MoveAnchor)

pos 에서 지정한 MoveMode 을 사용하여 m 에서 지정한 문서 내 절대 위치로 커서를 이동합니다. 커서는 문자 사이에 위치합니다.

참고: 이 경우 "문자"는 QChar 객체의 문자열, 즉 16비트 유니코드 문자를 의미하며 pos 은 이 문자열에 대한 인덱스로 간주됩니다. 대리 쌍, 언어 결합 또는 분음 부호 등의 경우처럼 하나의 문자가 여러 개의 유니코드 문자로 표현될 수 있으므로 이는 문자 체계의 개별 문자와 반드시 일치하지는 않습니다. 보다 일반적인 방법으로 문서를 탐색하려면 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.