2017-09-04 6 views
1

저는 PyQt5로 텍스트 편집기를 개발 중이며 "다음 찾기 ..."기능을 구현 중입니다. 사용자가 검색하고자하는 문자열을 입력합니다. "다음 찾기"버튼을 클릭 할 때마다 다음 일치하는 문자열이 강조 표시됩니다. Like thisPyQt5 - QTextEdit의 커서로 스크롤

내가했던 그 같은 QTextEdit.textCursor()를 사용하여 :

... 
textarea = QTextEdit() 
cursor = textarea.textCursor() 

#This function returns an array: [start index of the matched string, end index of the matched string] 
matched_string_indexes = findText(text_to_find, text,...) 

#So now I can use setPosition to select the matched string 
cursor.setPosition(array[0], QTextEdit.MoveAnchor) 
cursor.setPosition(array[1], QTextEdit.KeepAnchor) 

#Now that the matched string is seleted I can highlight it 
highlightText(cursor) 

일치하는 문자열 (뷰 포트에서) 페이지 하단에있는 경우 문제는, 내가 텍스트 영역을 아래로 (또는 위로) 자동으로 스크롤하려고합니다. QTextEdit의 ensureCursorVisible() 메서드로 시도했지만 작동하지 않습니다.

brute-force 솔루션은 scrollbar.setValue() 메서드를 사용하여 해당 행으로 스크롤하는 것보다 현재 행의 픽셀을 계산하는 것입니다.

textarea.ensureCursorVisible() 
#AND 
textare.setTextCursor(cursor) 

은 QTextEdit의 TextCursor를() 메소드가 커서의 카피를 돌려줍니다 아닌 진짜 그래서 우리는 setTextCursor() 메소드를 설정해야합니다 :

+1

확실하게'ensureCursorVisible()'이 작동하려면'textarea.setTextCursor (cursor)'를 호출해야합니다. – ekhumoro

+0

그것은 ... 나는 QTextEdit.textCursor() 메서드에 의해 반환 된 커서가 텍스트 영역의 커서로 "설정"되어야한다는 확신이 들었습니다. 정말 고마워. – NewBieBR

답변

2

사실, 난 단지에에 있습니다.

관련 문제