我试图在我的QTreeView中显示可单击的超链接。在
我可以用QLabels和QTreeView.setIndexWidget根据这个问题的建议。在
不幸的是,我的QTreeView可能相当大(有1000个项目),创建1000个qlabel的速度很慢。在
好处是我可以在QTreeView中使用一个委托来绘制看起来像超链接的文本。这太快了。在
现在的问题是,我需要它们像超链接一样响应(即鼠标悬停光标、响应单击等等),但我不确定最好的方法是什么。在
我可以通过连接到QTreeView的clicked()信号来伪造它,但它并不完全相同,因为它响应整个单元格,而不仅仅是单元格内的文本。在
最简单的方法似乎是通过子类化
QItemDelegate
,因为文本是由一个单独的虚函数drawDisplay
(使用QStyledItemDelegate
你几乎必须从头开始重新绘制项目,并且需要从QProxyStyle
派生的附加类):QTextDocument
和QTextDocument.documentLayout().draw()
绘制drawDisplay
,我们保存了绘制文本时的位置(因此保存的位置始终是鼠标所在项目的文本位置)editorEvent
中,以获取文档中鼠标的相对位置,并使用QAbstractTextDocumentLayout.anchorAt
获取文档中该位置的链接。在只要不启用按内容自动调整列大小(这将为每个项调用sizeHint),它似乎不会比没有委托慢。
对于自定义模型,可以通过在模型内直接缓存一些数据(例如,使用和存储QStaticText来代替QTextDocument)来加快速度。在
也许可以避免使用qlabel,但这可能会影响代码的可读性。在
可能不需要一次填满整棵树。您是否考虑过根据需要生成qlabel?分配足够的空间,用expand和expandAll信号覆盖子树。您可以通过创建一个qlabel池并根据需要更改它们的文本(以及它们的使用位置)来扩展它。在
谢谢你的代码,我在网上找到的越好。 我在我的项目中使用你的代码,但我需要使用qss样式表,你的代码不能工作。 我用QStyledItemDelegate替换了QItemDelegate,并修改了代码(html链接上的垂直对齐,可能是您可以找到另一个更简单的解决方法),并且只在字符串以'
别忘了连接项目委托:
^{pr2}$工作很棒!在
相关问题 更多 >
编程相关推荐