使用 PySide 和 QTextEdit 的半透明高光 [英] Semi-transparent highlights using PySide and QTextEdit
问题描述
我创建了一个 QTextEdit 对象.下面的代码为当前选定的文本添加了随机颜色的高光.我需要高光是半透明的,这样我才能看到高光相互叠加.使用setAlpha"似乎没有任何作用.如何设置高光的 alpha 或以其他方式获得半透明?
I've created a QTextEdit object. The code below adds randomly colored highlights to the currently selected text. I need the highlights to be semi-transparent so I can see highlights layered upon each other. Using "setAlpha" does not appear to do anything. How can I set the alpha for the highlight or otherwise obtain semi-transparency?
# Define cursor & span
self.cursor = self.textdoc.textCursor()
self.selstart = self.cursor.selectionStart()
self.selend = self.cursor.selectionEnd()
self.seltext = self.cursor.selectedText()
# Create random color
r = randint(0,255)
g = randint(0, 255)
b = randint(0, 255)
color = QColor(r,g,b)
color.setAlpha(125)
format = QTextCharFormat()
format.setBackground(color)
self.cursor.setCharFormat(format)
推荐答案
QTextEdit
似乎不太可能支持像分层格式这样复杂的东西.所以我认为你必须自己混合颜色.下面的例子使用了一个相当粗糙的方法,但它似乎工作正常.我不确定您的目标是什么结果,但它应该让您知道如何继续:
It seems unlikely that QTextEdit
would support anything as sophisticated as layered formatting. So I think you will have to do the blending of colours yourself. The example below uses a fairly crude method, but it seems to work okay. I'm not exactly sure what results you're aiming for, but it should give you some idea how to 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)
的键来查找所需的减去"颜色).
(PS: one thing I haven't attempted to implement here is removing highlights. If you used a relatively small set of colours, I suppose you could pre-compute a table of all colour combinations, and then use a key of (current_color, removed_color)
to look up the required "subtracted" colour).
这篇关于使用 PySide 和 QTextEdit 的半透明高光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!