2

I have a QML application that needs to react to a custom URL handler call, like "myapp://something". I know I need to register the URL handler in my *.desktop file as:

MimeType=x-scheme-handler/myapp;

However I do not know where to recieve the call to this handler in QML. Can someone help me?

Thanks.

3
  • Welcome to Ask Ubuntu. Please, could you put some of your time to read What should I do when someone answers my question? Apr 28, 2014 at 10:11
  • Don't worry. You will get your checkmark as soon as I can confirm your that your solution works. But there are some other problems I need to solve before trying this out. The biggest is that it is currently not possible to update my application in the Ubuntu One Marketplace and thus my motivation to continue coding on this particular project is quite low.
    – ali
    Apr 29, 2014 at 17:05
  • ok, no problem. It was just a friendly reminder Apr 29, 2014 at 17:19

1 Answer 1

1

Solution for 14.04 (Easy, QML only)

The following QML code will work on 14.04 only because it requires Qt >= 5.1 (and Trusty provides 5.2.1).

You can have a .desktop file containing:

Exec=/usr/bin/qmlscene %U /path/to/your/qml-app/myapp.qml
MimeType=x-scheme-handler/myapp

and use Qt.application.arguments[1] to receive the URL content.

Solution for 13.10 (requires C++)

The .desktop file is very similar to the previous one but here qmlscene is replaced by the name of your app:

Exec=/path/to/your/binary_app %U 
MimeType=x-scheme-handler/myapp

For 13.10, you'll have to use some C++ and parse the arguments using this type of application:

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;

    // Register the URL with the QML runtime
    viewer.rootContext()->setContextProperty("url", app.arguments().at(1));

    // Now, load the main page
    viewer.setMainQmlFile(QStringLiteral("qml/myapp.qml"));
    viewer.showExpanded();
    return app.exec();
}

You'll be able to get the url from QML thanks to setContextProperty().

Visit Embedding C++ Objects into QML with Context Properties for further details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .