[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] QDesktopServices.setUrlHandler not called on OS X
From:       Marko Luther <marko.luther () gmx ! net>
Date:       2019-06-30 14:34:11
Message-ID: 44EEE484-6E47-4E63-85C6-C8565D6BC947 () gmx ! net
[Download RAW message or body]

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


["setup.py" (setup.py)]

"""
Usage:
    python3 setup.py py2app
"""

from setuptools import setup
import os

APP = ['simple.py']
APP_NAME = "Simple"
OPTIONS = {}

OPTIONS = {
    'argv_emulation': False,
    'plist': {
        'test': 'test',
        'CFBundleName': APP_NAME,
        'CFBundleDisplayName': APP_NAME,
        'CFBundleGetInfoString': "Simple",
        'CFBundleIdentifier': "com.simple",
        'CFBundleVersion': "0.1.0",
        'CFBundleShortVersionString': "0.1.0",
        'NSHumanReadableCopyright': u"Copyright 2019",
        'CFBundleURLTypes': [
            {
                'CFBundleURLName' : 'com.simple',
                'CFBundleURLSchemes': ['test']
            }
        ]
    }
}

setup(
    name=APP_NAME,
    app=APP,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)
["simple.py" (simple.py)]

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QObject, Qt, QEvent


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

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

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)

if __name__ == '__main__':
    app = Simple(sys.argv)

    # custom URL schemes via setUrlHandler seem not to work
    #QDesktopServices.setUrlHandler('test', open_desktopservices_url)
    handler = URLHandler()
    QDesktopServices.setUrlHandler("test", handler.handleURL)

    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)

    if len(sys.argv) > 1:
        title = str(sys.argv[1])
    else:
        title = "simple"
    w.setWindowTitle(title)
    w.show()

    sys.exit(app.exec_())

[Attachment #5 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic