Search code examples
c++qtprotocolsqtwebkit

QtWebkit: how to handle custom protocols


I am writing an application using qt and qtWebkit, which is used to access a web application. In the web application we have various custom protocols which need to be handled e.g. 'myprotocol:var1=1&var2=2'. When this protocol is used, my application should open another program. How can I detect when one of these custom protocols is hit?

I have seen some answers to similar questions which involve 'reimplementing QWebPage::acceptNavigationRequest', but I don't know how to do this.

Has anyone else had to handle custom protocols in qtWebkit?


Solution

  • If you listen out for a 'finished(QNetworkReply *)' signal that calls a slot with with the below code in. You can get the URL of the protocol from the request contained within the reply. From there you can check if it's the protocol you want to launch and if it is you can launch the application using QDesktopServices::openUrl(reply->request().url());.

    QString scheme = reply->request().url().scheme();
    if (scheme == QLatin1String("mailto")
        || scheme == QLatin1String("myprotocol")) {
            QDesktopServices::openUrl(reply->request().url());
    }