QTextEdit
은 계층화 된 형식으로 정교한 것으로는 지원하지 않을 수 있습니다. 그래서 나는 당신이 자신의 색깔을 혼합해야한다고 생각합니다. 아래 예제는 매우 조잡한 방법을 사용하지만 제대로 작동하는 것 같습니다. 나는 당신이 겨냥하고있어 어떤 결과를 정확히 모르겠지만, 어떻게 proceeed하는 몇 가지 아이디어를 줄 것이다 :
import sys
from random import sample
from PySide import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtGui.QPushButton('Highlight', self)
self.button.clicked.connect(self.handleButton)
self.edit = QtGui.QTextEdit(self)
self.edit.setText(open(__file__).read())
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.edit)
layout.addWidget(self.button)
def blendColors(self, first, second, ratio=0.5, alpha=100):
ratio2 = 1 - ratio
return QtGui.QColor(
(first.red() * ratio) + (second.red() * ratio2),
(first.green() * ratio) + (second.green() * ratio2),
(first.blue() * ratio) + (second.blue() * ratio2),
alpha,
)
def handleButton(self):
cursor = self.edit.textCursor()
start = cursor.selectionStart()
end = cursor.selectionEnd()
if start != end:
default = QtGui.QTextCharFormat().background().color()
color = QtGui.QColor(*sample(range(0, 255), 3))
color.setAlpha(100)
for pos in range(start, end):
cursor.setPosition(pos)
cursor.movePosition(QtGui.QTextCursor.NextCharacter,
QtGui.QTextCursor.KeepAnchor)
charfmt = cursor.charFormat()
current = charfmt.background().color()
if current != default:
charfmt.setBackground(self.blendColors(current, color))
else:
charfmt.setBackground(color)
cursor.setCharFormat(charfmt)
cursor.clearSelection()
self.edit.setTextCursor(cursor)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())
(PS : 여기 구현하는 시도하지 않은 한 가지 이 하이라이트를 제거하는 것입니다 상대적으로 작은 색상 세트를 사용한 경우 모든 색상 조합의 표를 미리 계산 한 다음 (current_color, removed_color)
키를 사용하여 필요한 "빼기"색상을 찾습니다.
감사합니다. 코드가 디스플레이 문제를 해결합니다. 하이라이트 추가 및 제거의 복잡성에 대해 걱정하고 있습니다. textEdit의 알파는 일종의 레이어를 의미합니다. 맞습니까? 무엇을 겹칠 수 있습니까? 하이라이트의 레이어를 구체적으로 제공 할 수있는 textEdit 외에 Qt 위젯이 있습니까? – davideps
룩업 테이블은 얻을 수있는만큼 간단하고 효율적입니다. 레이어가 없습니다. 배경 그림 만 있습니다. 나는 그래픽 프레임 워크가 더 많은 가능성을 제공한다고 생각하지만, 이것만큼 기본적인 무언가를위한 헤비급 해결책으로 보인다. – ekhumoro
나는 계속 알파를 얻으려고 노력했다. QTextEdit 윈도우에서, setAlpha는 실제 투명도보다 채도 설정처럼 기능하는 것으로 보입니다. 당신의 대답은 도시에서 유일한 쇼처럼 보입니다. 고맙습니다. – davideps