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

Darren Dale dsdale24 at gmail.com
Fri Dec 5 20:51:24 GMT 2014


Perfect, thank you. I'm doing something along the lines of:

        temp = WhatsThisUrlLoader()
        for k, v in self.__dict__.items():
            if isinstance(v, QWidget):
                v.installEventFilter(temp)

with:


class WhatsThisUrlLoader(QObject):

    def eventFilter(self, e):
        if e.type() == QEvent.WhatsThisClicked:
            webbrowser.open_new_tab(e.href())
        return False


On Fri, Dec 5, 2014 at 10:27 AM, Florian Bruhin <me at the-compiler.org> wrote:

> * 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
>
> Florian
>
> --
> 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/
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141205/823100dd/attachment-0001.html>


More information about the PyQt mailing list