- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个 QTextEdit 对象。下面的代码为当前选定的文本添加了随机颜色的突出显示。我需要高光是半透明的,这样我才能看到高光相互叠加。使用“setAlpha”似乎没有做任何事情。如何为高光设置 alpha 或以其他方式获得半透明?
# 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
似乎不太可能支持像分层格式这样复杂的任何东西。所以我认为你必须自己混合颜色。下面的示例使用了一种相当粗糙的方法,但它似乎工作正常。我不确定您的目标是什么结果,但它应该让您了解如何继续:
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)
的键来查找所需的“减去”颜色)。
关于python - 使用 PySide 和 QTextEdit 的半透明高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46628690/
在这个 fiddle 上,函数完美地完成了工作,但我需要稍微调整连接。 该点击甚至需要是父 div 的 addClass(深色/浅色)(true 或 false)。 如果是深色,则添加 Bright
我正在使用 Windows.UI.ViewManagement.UISettings 来获取系统强调色,但该类似乎没有任何亮/暗模式的方法或属性。我找不到此功能的文档,我该如何检测? PS:我正在制作
在使用 iOS 13 在明暗模式之间切换时,我遇到了显示键盘的异常行为。 该 View 有一个 inputAccessoryView 供用户输入消息。当然后在键盘显示的情况下切换亮/暗模式时,亮/暗模
我正在开发一个支持多个主题的 iOS 应用程序。有些主题使用深色背景,有些使用浅色。一些默认图标在深色/浅色背景中不可见。我在 xcassets 中看到一个选项,可以为不同的颜色模式添加图像。我的目标
一些上下文:Sciter (纯 win32 应用程序)已经能够呈现类似 UWP 的 UI: 在深色模式下: 在灯光模式下: Windows 10.1803 在设置小程序中引入深色/浅色开关 as se
我是一名优秀的程序员,十分优秀!