Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:57:01+00:00 2026-05-25T05:57:01+00:00

I’m trying to display clickable hyperlinks in my QTreeView. I was able to do

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:57:02+00:00Added an answer on May 25, 2026 at 5:57 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.