下面的代码创建了一个突出显示位置的数据库,并将它们应用于QTextEdit窗口中的字符串。我有一个关于半透明高光的separate question。在这里,我想了解如何实现"鼠标悬停"返回数据库列表的功能" id"亮点。 QHoverEvent似乎不是这项工作的正确工具,因为它在小部件级别起作用,因此无法检测到高亮显示。有什么我应该用的吗?
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
def create_database():
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("highlights.sqlite")
db.open()
q = QSqlQuery()
q.exec_("CREATE TABLE highlights(id INTEGER PRIMARY KEY, start INTEGER, end INTEGER, r INTEGER, g INTEGER, b INTEGER)")
q.exec_("INSERT INTO highlights VALUES(1,0,20,100,200,100)")
q.exec_("INSERT INTO highlights VALUES(2,25,40,200,100,100)")
q.exec_("INSERT INTO highlights VALUES(3,35,50,100,100,200)")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.center = QMainWindow(self)
self.textdoc = QTextEdit("After the creation of the application object, we have created a QLabel object. A QLabel is a widget that can present text (simple or rich, like html), and images. Note that after the creation of the label, we are calling the method show which will present the label to the user.")
self.setCentralWidget(self.textdoc)
def load_highlights():
cursor = mainwindow.textdoc.textCursor()
model = QSqlTableModel()
model.setTable("highlights")
model.select()
count = model.rowCount()
for n in range(count):
record = model.record(n)
start = record.value('start')
end = record.value('end')
r = record.value('r')
g = record.value('g')
b = record.value('b')
color = QColor(r, g, b)
cursor.setPosition(start)
cursor.setPosition(end, QTextCursor.KeepAnchor)
mainwindow.textdoc.setTextCursor(cursor)
format = QTextCharFormat()
format.setBackground(color)
mainwindow.textdoc.setCurrentCharFormat(format)
if __name__ == '__main__':
create_database()
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
load_highlights()
sys.exit(app.exec_())
app.exec_()
db.close()