[PyQt] QDesktopServices.setUrlHandler not called on OS X

Marko Luther marko.luther at gmx.net
Sun Jun 30 15:34:11 BST 2019


Dear all,

I am struggling to get the expected call-back from a URL handler registered via QDesktopServices.setUrlHandler on opening the registered URL scheme (test://<path>).

The scheme is registered via the following info.plist entry 

	<key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleURLName</key>
			<string>com.simple</string>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>test</string>
			</array>
		</dict>
	</array>

The handler is installed in the PyQt script via

QDesktopServices.setUrlHandler('test', open_desktopservices_url)

with 

def open_desktopservices_url(url):
    print("open",url)

or

handler = URLHandler()
QDesktopServices.setUrlHandler("test", handler.handleURL) 

class URLHandler(QObject):
    def handleURL(self, url):
        open_desktopservices_url(url)


To open the URL I put the URL "test://me" into Safari or use the terminal with

# open test://me

Opening the URL brings the app to the foreground, but the handler is never called.

I found a way to catch this URL request by catching a FileOpen event and extracting the given URL from the event as follows

class Simple(QApplication):
    def __init__(self, args):
        super(Simple, self).__init__(args)

    def event(self, event):
        if event.type() == QEvent.FileOpen:
            url = event.url()
            if url.isValid():
                print("url",event.url())
        return super(Simple, self).event(event)


Is this the way it has to be done?

I attached a minimal sample app sample.py together with a setup.py script to build the app with py2app that builds and runs as follows:

# python3 setup.py py2app
# dist/Simple.app/Contents/MacOS/Simple

This is on OS X 10.13.6, Python 3.7.3, PyQt 5.12.13, py2app 0.19

Thanks for any pointer in the right direction,
Marko

-------------- next part --------------
A non-text attachment was scrubbed...
Name: setup.py
Type: text/x-python-script
Size: 796 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190630/88cd64b4/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simple.py
Type: text/x-python-script
Size: 1171 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190630/88cd64b4/attachment-0001.bin>


More information about the PyQt mailing list