[PyQt] How to open QWhatsThis hyperlinks in an external browser?

Florian Bruhin me at the-compiler.org
Fri Dec 5 15:27:34 GMT 2014

* Darren Dale <dsdale24 at gmail.com> [2014-12-05 10:06:53 -0500]:
> For many widgets in a GUI I'm developing, I would like to add What's This
> documentation that contain links to online Sphinx-based documentation. In
> Designer, I can add whatsThis documentation with hyperlinks, but clicking
> the link in the resulting GUI doesn't do anything. Based on the
> documentation for QWhatsThis (
> http://pyqt.sourceforge.net/Docs/PyQt4/qwhatsthis.html), and some searching
> online (http://qt-project.org/forums/viewthread/34565), my understanding is
> that I would need to reimplement the `event` method for each widget,
> perhaps along the lines of:
>     def event(self, e):
>         try:
>             href = e.href()
>             import webbrowser
>             webbrowser.open_new_tab(href)
>         except AttributeError:
>             pass
>         super(self.__class__, self).event(e)

You'd also have to check if the event actually is a QWhatsThisEvent
(e.type() == QEvent.WhatsThisClicked).  Then the try/except is
probably unnecessary as well.

> On the other hand, I'm using Designer and uic.loadUi, so the prospect of
> subclassing numerous widgets is... I don't know how I would do it. And the
> prospect of rewriting the entire GUI programmatically to support this
> rather obvious use case makes me suspect I'm just doing it wrong.
> Is it possible to plug in to PyQt's uic/loadUi routines to define the
> `event` method for every widget that gets instantiated? Or is there another
> approach I have overlooked?

You could use an event filter[1] on the QApplication to intercept all
WhatsThisClicked events and react on them.

[1] http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters


http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
             GPG 0xFD55A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141205/9d988443/attachment.sig>

More information about the PyQt mailing list