2016-09-16 2 views
0

단일 문자에 대한 QTextCharFormat을 업데이트하려고합니다. 하지만이 적용되지 않습니다 :QTextEdit single QTextCharFormat

QTextCursor cursor(document()); 
cursor.setPosition(4); 
QTextCharFormat format; 
format.setFontPointSize(sizeString.toInt()); 
cursor.mergeCharFormat(format); 
qDebug() << "SET POS " << cursor.position() << " TO " << sizeString.toInt(); 

QTextCursor cursor2(document()); 
cursor.setPosition(4); 
QTextCharFormat charformat = cursor2.charFormat(); 
QFont font = charformat.font(); 
qDebug() << " LOADED FONTSIZE: " << font.pointSize(); 

출력 :

SET POS 4 TO 16 
LOADED FONTSIZE: 36 

어떤 생각이 무엇을 놓치고? 올바른 사용의

+0

커서에 익숙하지 않지만 선택 (moveCursor 및 앵커를 유지하여 일부 텍스트를 선택해야 함)하지 않아도됩니까? – Hayt

+0

난 그냥이 시도 .. 내가 변경된 QTextCharFormat 적용하려면 오른쪽 함수가 누락 된 것 같아 – wutzebaer

+1

여기 예제를 확인하십시오 : http://doc.qt.io/qt-5/richtext-cursor.html " 서식있는 텍스트 조작 " – Hayt

답변

2

적용 할 변경 사항의 일부를 선택해야합니다. 텍스트 (실제 편집기에서와 같이). 실제로 물건을 선택하지 않고 커서를 위치로 설정 만합니다.

텍스트를 선택하려면 선택을 시작하면서 커서를 다른 위치로 이동해야합니다.

cursor.setPosition(4); 
cursor.setPosition(5, QTextCursor::KeepAnchor); 

이렇게하면 커서가 위치 4에 설정됩니다. 커서를 위치 5로 이동 시키지만 선택 앵커는 유지합니다. 위치 4와 5 사이의 모든 것이 선택됩니다.

변경 사항이 선택 항목에 적용됩니다.

0

예 :

당신은 QTextEdit의 커서를 가져

QTextEdit *editor = new QTextEdit(); 
QTextCursor cursor(editor->textCursor()); 
cursor.movePosition(QTextCursor::Start); 

다른 형식

를 사용하여 텍스트 편집에 텍스트를 삽입 이제 다른 QTextCharFormat

QTextCharFormat plainFormat(cursor.charFormat()); 

QTextCharFormat headingFormat = plainFormat; 
headingFormat.setFontWeight(QFont::Bold); 
headingFormat.setFontPointSize(16); 

QTextCharFormat emphasisFormat = plainFormat; 
emphasisFormat.setFontItalic(true); 

설정

cursor.insertText(tr("Character formats"), 
        headingFormat); 

cursor.insertBlock(); // Single character 
cursor.insertText(tr("a"), emphasisFormat); 
cursor.insertText(tr("b"), headingFormat); 
cursor.insertBlock(); 


cursor.insertText(tr("Text can be displayed in a variety of " 
           "different character formats. "), plainFormat); 
cursor.insertText(tr("We can emphasize text by ")); 
cursor.insertText(tr("making it italic"), emphasisFormat); 

다른 스타일의 텍스트를 렌더링하는 즉시 편집 가능한 위젯의 스타일을 변경하려는 경우이 URL의 예가 있습니다 : Syntax Highlighter Example

+0

기존 텍스트의 QTextCharFormat을 변경하는 방법에 대한 예제가 있습니까? – wutzebaer

+0

아니요, 죄송합니다. 첨부 된 링크를 확인하십시오. – mohabouje