python-使用PySide2在QTableView中设置文本样式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-使用PySide2在QTableView中设置文本样式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3577字,纯文字阅读大概需要6分钟。
内容图文
我有一个填充了适当模型的QTableView.
我想根据上下文更改文本样式:
文本,如果括号之间有一些内容,则文本的这一部分应更改为绿色-包括括号-(并加粗,即使不难使用).
如果您可以提供摘要或超级简单的示例,我将不胜感激.
解决方法:
您必须使用使用QTextDocument的委托:
import random
from PySide2 import QtCore, QtGui, QtWidgets
words = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris euismod cursus mi sit amet pellentesque.
Proin sed lectus sed augue scelerisque congue eget quis leo.
Curabitur ultrices nisi nisi, placerat gravida urna sagittis et.
Nullam vitae urna tortor. Curabitur a lobortis metus, et laoreet arcu.
Quisque a mi in purus molestie porta non sit amet purus.
Sed porta non purus et suscipit.'''.split()
class HighlightDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super(HighlightDelegate, self).__init__(parent)
self.doc = QtGui.QTextDocument(self)
self._regex = QtCore.QRegularExpression()
self._highlight_format = QtGui.QTextCharFormat()
def paint(self, painter, option, index):
painter.save()
options = QtWidgets.QStyleOptionViewItem(option)
self.initStyleOption(options, index)
self.doc.setPlainText(options.text)
self.apply_highlight()
options.text = ""
style = QtWidgets.QApplication.style() if options.widget is None \
else options.widget.style()
style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter)
ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
if option.state & QtWidgets.QStyle.State_Selected:
ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
QtGui.QPalette.Active, QtGui.QPalette.HighlightedText))
else:
ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
QtGui.QPalette.Active, QtGui.QPalette.Text))
textRect = style.subElementRect(
QtWidgets.QStyle.SE_ItemViewItemText, options)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
the_constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - the_constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def apply_highlight(self):
cursor = QtGui.QTextCursor(self.doc)
cursor.beginEditBlock()
highlightCursor = QtGui.QTextCursor(self.doc)
while not highlightCursor.isNull() and not highlightCursor.atEnd():
highlightCursor = self.doc.find(self.regex, highlightCursor)
if not highlightCursor.isNull():
highlightCursor.mergeCharFormat(self.highlightFormat)
cursor.endEditBlock()
@property
def regex(self):
return self._regex
@regex.setter
def regex(self, regex):
if self._regex == regex: return
self._regex = regex
@property
def highlightFormat(self):
return self._highlight_format
@highlightFormat.setter
def highlightFormat(self, fmt):
self._highlight_format = fmt
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.table = QtWidgets.QTableView()
self._delegate = HighlightDelegate(self.table)
self._delegate.regex = QtCore.QRegularExpression(r"\(.*?\)")
fmt = QtGui.QTextCharFormat()
fmt.setForeground(QtCore.Qt.green)
fmt.setFontWeight(QtGui.QFont.Bold)
self._delegate.highlightFormat = fmt
self.table.setItemDelegate(self._delegate)
model = QtGui.QStandardItemModel(10, 4)
for i in range(model.rowCount()):
for j in range(model.columnCount()):
item = QtGui.QStandardItem("{}({}){}".format(*random.sample(words,3)))
model.setItem(i, j, item)
self.table.setModel(model)
self.setCentralWidget(self.table)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
内容总结
以上是互联网集市为您收集整理的python-使用PySide2在QTableView中设置文本样式全部内容,希望文章能够帮你解决python-使用PySide2在QTableView中设置文本样式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。