没有QLabel的QTreeView中的超链接 [英] Hyperlinks in QTreeView without QLabel

查看:227
本文介绍了没有QLabel的QTreeView中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在QTreeView中显示可点击的超链接。



根据这个问题的推荐,我能够使用QLabels和QTreeView.setIndexWidget来做到这一点。



< a href =https://stackoverflow.com/questions/7087064/hyperlinks-in-qtreeview> QTreeView中的超链接



不幸的是,我的QTreeView可以相当大(1000个项目),并且创建1000个QLabel很慢。



好处是我可以在我的QTreeView中使用Delegate来绘制看起来像一样的超链接。这是非常快的。



现在的问题是我需要它们像超链接一样响应(即鼠标悬停手形光标,响应点击等),但我是不知道最好的方法是什么。



我已经能够通过连接到QTreeView的clicked()信号来伪装它,但它并不完全相同,因为它响应整个单元格,而不仅仅是单元格内的文本。

解决方案

最简单的方法似乎是通过继承 QItemDelegate ,因为文本是由一个单独的虚函数绘制的, drawDisplay (使用 QStyledItemDelegate 你几乎不得不从头开始重绘项目,你需要一个派生自 QProxyStyle 的额外类:




  • 使用 QTextDocument 绘制HTML文本当鼠标e时,QTextDocument.documentLayout()。draw()

  • nters一个项目,重新绘制相同的项目并调用 drawDisplay ,我们保存位置,我们正在绘制文本(因此保存的位置始终是文本的位置鼠标所在的项目,

  • 该位置用于 editorEvent 以获取鼠标在文档中的相对位置并使用 QAbstractTextDocumentLayout.anchorAt 获取文档中该位置的链接。



< pre class =lang-py prettyprint-override> 从PySide.QtCore导入sys
导入*
来自PySide.QtGui import *

class LinkItemDelegate (QItemDelegate):
linkActivated = Signal(str)
linkHovered = Signal(str)#连接到QStatusBar.showMessage插槽

def __init __(self,parentView):
QItemDelegate .__ init __(self,parentView)
assert isinstance(parentView,QAbstractItemView),\
第一个参数必须是视图

#我们需要在editEvent中接收鼠标移动事件
parentView.setMouseTracking(True)

#鼠标移动时恢复鼠标光标t超过
#项目但仍在视图小部件上
parentView.viewportEntered.connect(parentView.unsetCursor)

#documents [0]将包含上次悬停的文档item
#documents [1]将用于绘制普通(未悬停)项目
self.documents = []
for i in range(2):
self.documents .append(QTextDocument(self))
self.documents [i] .setDocumentMargin(0)
self.lastTextPos = QPoint(0,0)

def drawDisplay(self, painter,option,rect,text):
#因为状态只告诉鼠标是否在
#行上,我们必须检查它是否超过了项目
mouseOver = option。国家与QStyle.State_MouseOver \
和rect.contains(self.parent()。viewport()\
.mapFromGlobal(QCursor.pos()))\
和option.state& ; QStyle.State_Enabled

如果mouseOver:
#使用文档[0]并保存editorEvent的文本位置
doc = self.documents [0]
self.lastTextPos = rect.topLeft()
doc.setDefaultStyleSheet()
else:
doc = self.documents [1]
#链接默认装饰,因此禁用它
#当鼠标不在项目上时
doc.setDefaultStyleSheet(a {text-decoration:none})

doc.setDefaultFont(option.font)
doc.setHtml(text)

painter.save()
painter.translate(rect.topLeft())
ctx = QAbstractTextDocumentLayout.PaintContext()
ctx .palette = option.palette
doc.documentLayout()。draw(painter,ctx)
painter.restore()

def editorEvent(self,event,model,option, index):
如果event.type()不在[QEvent。 MouseMove,QEvent.MouseButtonRelease] \
与否(option.state& QStyle.State_Enabled):
返回False
#获取鼠标位置的链接
#(只有PyQt需要显式QPointF转换)
pos = QPointF(event.pos () - self.lastTextPos)
anchor = self.documents [0] .documentLayout()。anchorAt(pos)
if anchor ==:
self.parent()。unsetCursor ()
else:
self.parent()。setCursor(Qt.PointingHandCursor)
if event.type()== QEvent.MouseButtonRelease:
self.linkActivated.emit(锚)
返回True
else:
self.linkHovered.emit(anchor)
return False

def sizeHint(self,option,index):
#原始大小是根据带有html标签
#的字符串计算的,所以我们需要从中减去带有和的文本宽度
#之间的差异没有html标签
size = QItemDelegate.sizeHint(self,option,index)

#使用QTextDocument去除标签
doc = self.documents [1]
html = index.data()#必须为PyQt添加.toString()API 1
doc.setHtml(html)
plainText = doc.toPlainText()

fontMetrics = QFontMetrics(option.font)
diff = fontMetrics.width(html) - fontMetrics.width(plainText)

返回大小 - QSize(diff,0)

只要你没有启用自动列调整内容大小(这会为每个项目调用sizeHint),它就不会似乎比没有代表更慢。
使用自定义模型,可以通过直接缓存模型中的一些数据来加速它(例如,通过使用和存储QStaticText用于非悬停项而不是QTextDocument)。


I'm trying to display clickable hyperlinks in my QTreeView.

I was able to do this using QLabels and QTreeView.setIndexWidget per the recommendations from this question.

Hyperlinks in QTreeView

Unfortunately, my QTreeView can be rather large (1000s of items), and creating 1000s of QLabels is slow.

The upside is that I can use a Delegate in my QTreeView to draw text that looks like hyperlinks. This is super fast.

The problem now is that I need them to respond like hyperlinks (i.e. mouseover hand cursor, respond to clicks, etc.), but I'm not sure what the best way to go about that is.

I've been able to sort of fake it by just connecting to the clicked() signal of the QTreeView, but it's not exactly the same, because it responds to the whole cell, and not just the text inside the cell.

解决方案

The easiest way to do that seems to be by subclassing QItemDelegate, because the text is drawn by a separate virtual function, drawDisplay (with QStyledItemDelegate you would almost have to redraw the item from scratch and you would need an additional class deriving from QProxyStyle):

  • the HTML text is drawn with QTextDocument and QTextDocument.documentLayout().draw(),
  • when the mouse enters an item, that same item is repainted and drawDisplay is called, we save the position were we are drawing the text (so the saved position is always the position of the text for the item over which the mouse is),
  • that position is used in editorEvent to get the relative position of the mouse inside the document and to get the link at that position in the document with QAbstractTextDocumentLayout.anchorAt.

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class LinkItemDelegate(QItemDelegate):
    linkActivated = Signal(str)
    linkHovered = Signal(str)  # to connect to a QStatusBar.showMessage slot

    def __init__(self, parentView):
        QItemDelegate.__init__(self, parentView)
        assert isinstance(parentView, QAbstractItemView), \
            "The first argument must be the view"

        # We need that to receive mouse move events in editorEvent
        parentView.setMouseTracking(True)

        # Revert the mouse cursor when the mouse isn't over 
        # an item but still on the view widget
        parentView.viewportEntered.connect(parentView.unsetCursor)

        # documents[0] will contain the document for the last hovered item
        # documents[1] will be used to draw ordinary (not hovered) items
        self.documents = []
        for i in range(2):
            self.documents.append(QTextDocument(self))
            self.documents[i].setDocumentMargin(0)
        self.lastTextPos = QPoint(0,0)

    def drawDisplay(self, painter, option, rect, text): 
        # Because the state tells only if the mouse is over the row
        # we have to check if it is over the item too
        mouseOver = option.state & QStyle.State_MouseOver \
            and rect.contains(self.parent().viewport() \
                .mapFromGlobal(QCursor.pos())) \
            and option.state & QStyle.State_Enabled

        if mouseOver:
            # Use documents[0] and save the text position for editorEvent
            doc = self.documents[0]                
            self.lastTextPos = rect.topLeft()
            doc.setDefaultStyleSheet("")
        else:
            doc = self.documents[1]
            # Links are decorated by default, so disable it
            # when the mouse is not over the item
            doc.setDefaultStyleSheet("a {text-decoration: none}")

        doc.setDefaultFont(option.font)
        doc.setHtml(text)

        painter.save()
        painter.translate(rect.topLeft())
        ctx = QAbstractTextDocumentLayout.PaintContext()
        ctx.palette = option.palette
        doc.documentLayout().draw(painter, ctx)
        painter.restore()

    def editorEvent(self, event, model, option, index):
        if event.type() not in [QEvent.MouseMove, QEvent.MouseButtonRelease] \
            or not (option.state & QStyle.State_Enabled):
            return False                        
        # Get the link at the mouse position
        # (the explicit QPointF conversion is only needed for PyQt)
        pos = QPointF(event.pos() - self.lastTextPos)
        anchor = self.documents[0].documentLayout().anchorAt(pos)
        if anchor == "":
            self.parent().unsetCursor()
        else:
            self.parent().setCursor(Qt.PointingHandCursor)               
            if event.type() == QEvent.MouseButtonRelease:
                self.linkActivated.emit(anchor)
                return True 
            else:
                self.linkHovered.emit(anchor)
        return False

    def sizeHint(self, option, index):
        # The original size is calculated from the string with the html tags
        # so we need to subtract from it the difference between the width
        # of the text with and without the html tags
        size = QItemDelegate.sizeHint(self, option, index)

        # Use a QTextDocument to strip the tags
        doc = self.documents[1]
        html = index.data() # must add .toString() for PyQt "API 1"
        doc.setHtml(html)        
        plainText = doc.toPlainText()

        fontMetrics = QFontMetrics(option.font)                
        diff = fontMetrics.width(html) - fontMetrics.width(plainText)

        return size - QSize(diff, 0)

As long as you don't enable the automatic column resizing to contents (which would call sizeHint for every items), it doesn't seem to be slower than without the delegate.
With a custom model, it might be possible to speed it up by caching directly some data inside the model (for example, by using and storing QStaticText for non hovered items instead of QTextDocument).

这篇关于没有QLabel的QTreeView中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆