【问题标题】:In PyQt QTreeView item, how to set color for specific column blocks在 PyQt QTreeView 项目中,如何为特定列块设置颜色
【发布时间】:2017-04-23 16:12:15
【问题描述】:

使用基于 PyQt4 的 QTreeView,我创建了 2 个 xml 树小部件。从两棵树中,想要比较选定的项目并突出显示差异。例如,

左字符串:“比较字符串” 右字符串:“ComPareStringRight”

对差异的观察:

  • Left[0:2] 与 Right[0:2] 相同
  • 左[3:3] 与右[3:3] 不同
  • 左[4-12]与右[4-12]相同
  • Left[13-17] 不存在于 Left

现在,要根据以下设置颜色:

  • 匹配字符 - 默认
  • 不同的字符 - 橙色
  • 添加字符 - 绿色
  • 已删除字符 - 红色

我该如何实现呢?无法找到任何可供参考的参考实现。请提出前进的方向。

class QCustomDelegate (QItemDelegate):
  global showDiffPaint
  def paint (self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex):
    column = indexQModelIndex.column()
    if showDiffPaint == 1:
      QItemDelegate.paint(self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex)
    else:
      QItemDelegate.paint(self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex)

【问题讨论】:

    标签: pyqt qtreeview


    【解决方案1】:

    经过挖掘,发现将文本转换为html很有用,并呈现如下。 但是,由于 " 和 字符显示而发现了错误。我认为我需要以某种方式逃避......

          options = QStyleOptionViewItemV4(option)
          doc = QTextDocument()
          doc.setHtml(txt1)
          doc.setTextWidth(option.rect.width())
          style = QApplication.style()
          style.drawControl(QStyle.CE_ItemViewItem, options, painter)
          ctx = QAbstractTextDocumentLayout.PaintContext()
          textRect = style.subElementRect(QStyle.SE_ItemViewItemText,
                                        options)
          painter.translate(textRect.topLeft())
          painter.setClipRect(textRect.translated(-textRect.topLeft()))
          doc.documentLayout().draw(painter, ctx)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2015-11-20
      • 1970-01-01
      • 2018-04-29
      相关资源
      最近更新 更多