使用PySide和的QTextEdit(版本2)
半透明亮点我试图使semi-transparent highlights使用列表为一个更完整的示例:使用PySide和的QTextEdit(版本2)
(1)聚合的R,G,B,A,并突出显示计数(五个列表)
(2)平均的R,G,B,A(四个列表)
在平均列表中的值获得分配为背景色。聚合列表用于通过添加和减去高亮颜色来计算平均值。每个列表都有与文本小部件中的字符一样多的元素。
我正在使用一个熊猫数据框在内存中存储高亮,因为可能有很多它们以各种方式以及其他数据最终处理。将鼠标放在字符上可以打印出它的位置,亮点数量和RGBA值。除了有时会弹出一个问题 - 就像从一个高光位置移动到另一个高光位置时,画笔被拖动一秒钟一样。查看文本“PySide.QtCore”末尾“e”处的绿松石颜色。我认为问题在于我如何使用设置和移动光标的位置 - 但我不确定。添加每个突出显示后,是否需要重置光标的位置?我没有正确选择单个字符吗?
import sys
import pandas as pd
import sqlite3
from PySide.QtCore import *
from PySide.QtGui import *
def connect_database(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
def create_data(connection): # database connection
c = connection.cursor() # database cursor
c.execute("CREATE TABLE sections(id INTEGER PRIMARY KEY, start INTEGER, end INTEGER, r INTEGER, g INTEGER, b INTEGER, a INTEGER)")
c.execute("INSERT INTO sections VALUES(1,0,20,100,200,100,100)")
c.execute("INSERT INTO sections VALUES(2,15,20,200,100,100,50)")
c.execute("INSERT INTO sections VALUES(3,18,30,100,100,200,100)")
c.execute("INSERT INTO sections VALUES(4,50,60,100,200,200,150)")
db.commit()
return c.lastrowid
class QTextEdit2(QTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
self.cursor = self.textCursor()
self.length = len(self.toPlainText())
self.bg_red = [0 for n in range(self.length)] # stores aggregate values of all highlights (not averages)
self.bg_green = [0 for n in range(self.length)]
self.bg_blue = [0 for n in range(self.length)]
self.bg_alpha = [0 for n in range(self.length)]
self.bg_count = [0 for n in range(self.length)] # number of highlights. if this is 0 then don't display
# stored r,g,b just display white. in this example
# only highlights are written. everything else stays
# default
self.display_red = [0 for n in range(self.length)] # set to the value to display (average of highlights)
self.display_green = [0 for n in range(self.length)]
self.display_blue = [0 for n in range(self.length)]
self.display_alpha = [0 for n in range(self.length)]
self.sections = self.load_sections()
self.color_sections()
def mouseMoveEvent(self, event):
point = QPoint()
x = event.x()
y = event.y()
point.setX(x)
point.setY(y)
n = self.cursorForPosition(point).position()
print("%d: Section Count: %d RGBA: %d %d %d %d" % (n, self.bg_count[n],self.display_red[n], self.display_green[n],self.display_blue[n], self.display_alpha[n]))
super().mouseMoveEvent(event)
def load_sections(self):
c = sqlite3.connect("qda_test_01.sqlite")
df = pd.read_sql_query("SELECT * FROM sections", c)
return df
def blend_colors(self, start, end, r, g, b, a):
for n in range(start,end):
self.bg_red[n] = self.bg_red[n]+r
self.bg_green[n] = self.bg_green[n]+g
self.bg_blue[n] = self.bg_blue[n]+b
self.bg_alpha[n] = self.bg_alpha[n]+a
self.bg_count[n] = self.bg_count[n]+1
self.display_red[n] = self.bg_red[n]/self.bg_count[n]
self.display_green[n] = self.bg_green[n]/self.bg_count[n]
self.display_blue[n] = self.bg_blue[n]/self.bg_count[n]
self.display_alpha[n] = self.bg_alpha[n]/self.bg_count[n]
if self.display_red[n] > 255: # just in case RGBA data is weird...
self.display_red[n] = 255
if self.display_green[n] > 255:
self.display_green[n] = 255
if self.display_blue[n] > 255:
self.display_blue[n] = 255
if self.display_alpha[n] > 255:
self.display_alpha[n] = 255
if self.display_red[n] < 0:
self.display_red[n] = 0
if self.display_green[n] < 0:
self.display_green[n] = 0
if self.display_blue[n] < 0:
self.display_blue[n] = 0
if self.display_alpha[n] < 0:
self.display_alpha[n] = 0
print("LOCATION: %d | SECTION: r:%d g:%g b:%d a:%d | DISPLAY: r:%d g:%g b:%d a:%d" % (n,self.bg_red[n],self.bg_green[n],self.bg_blue[n],self.bg_alpha[n],self.display_red[n],self.display_green[n],self.display_blue[n],self.display_alpha[n]))
color = QColor(self.display_red[n], self.display_green[n], self.display_blue[n])
color.setAlpha(self.display_alpha[n])
cursor = self.textCursor()
cursor.setPosition(n)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, 1)
charfmt = cursor.charFormat()
charfmt.setBackground(color)
self.setCurrentCharFormat(charfmt)
self.setTextCursor(cursor)
def color_sections(self):
for n in range(self.sections.id.count()):
print("-----SECTION:%d-----" % (n))
section = self.sections.iloc[n]
self.blend_colors(section.start, section.end, section.r, section.g, section.b, section.a)
if __name__ == '__main__':
# Create database and sections to highlight
fn='qda_test_01.sqlite'
db=connect_database(fn)
id=create_data(db)
db.close()
app = QApplication(sys.argv)
window = QTextEdit2(
"In addition, the PySide.QtCore.QPoint class provides the PySide.QtCore.QPoint.manhattanLength() function which gives an inexpensive approximation of the length of the PySide.QtCore.QPoint object interpreted as a vector. Finally, PySide.QtCore.QPoint objects can be streamed as well as compared.")
window.show()
sys.exit(app.exec_())
如果你在我的previous answer用我给你的代码,目前的例子就是正常工作。实际上,您的更改引入了大量逐个错误。
要调试这个,你应该首先检查确切地看到应该突出显示哪些文本块。以文本并从数据库中开始/结束值的前65个字符,这给:
>>> t = "In addition, the PySide.QtCore.QPoint class provides the PySide."
>>> t[0:20]
'In addition, the PyS'
>>> t[15:20]
'e PyS'
>>> t[18:30]
'ySide.QtCore'
>>> t[50:60]
'es the PyS'
如果你比较这对实际产出的亮点,你会看到,没有任何部分匹配(例如,在每个“PySide”中查看“S”)。
为了得到这个正常工作,你必须在一开始获得的文本光标一次,用它来进行所有必要的修改,然后重新设置末一次:
cursor = self.textCursor()
for n in range(start, end):
...
cursor.setPosition(n)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
charfmt = cursor.charFormat()
charfmt.setBackground(color)
cursor.setCharFormat(charfmt)
cursor.clearSelection()
self.setTextCursor(cursor)
这与更新数据库类似:您使用游标来安排一系列更改,然后在最后将其作为单个操作进行提交。
你的代码纠正了这个问题,你的解释是非常有用的,尤其是指出setTextCursor类似于数据库提交。鉴于上下文是一个文本编辑部件,我期望“光标”的行为与文字处理器中的光标相同,并且cursor.setCharFormat立即产生效果。当我试图让我的代码工作时,我进一步从前面的代码中冒险。 – davideps
这是什么应用?突出显示的目的究竟是什么?没有任何上下文,你的代码很难理解。 – ekhumoro
我的代码也很难理解,因为我是Python的新手,在编程时会生锈。每个突出显示代表一个或多个研究人员分配的类别。研究人员将对重点进行重新分类甚至删除,这些重点常常重叠几层。用户可以根据自己的喜好更改颜色。在社会科学中,这个过程被称为“[编码](https://en.wikipedia.org/wiki/Coding_(social_sciences))”访谈。我怀疑我的问题是在setPosition,movePosition,设置格式行。但是,如果代码太难读,我可以尝试一个更基本的例子并重新发布。 – davideps
只有一个突出显示时,代码似乎可以正常工作。所以,当它重复的过程中,某些事情会发生错误。 – davideps