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.
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(withQStyledItemDelegateyou would almost have to redraw the item from scratch and you would need an additional class deriving fromQProxyStyle):QTextDocumentandQTextDocument.documentLayout().draw(),drawDisplayis 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),editorEventto get the relative position of the mouse inside the document and to get the link at that position in the document withQAbstractTextDocumentLayout.anchorAt.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).