Search code examples
windowsqtregistry

Launch QT App from Custom Protocol - Windows


I am looking for help in being able to launch a QT app that I am working on from a custom protocol (such as myapp://something). I have been successful in doing this on Mac; however, doing it on Windows has proved more challenging for me.

I have tried setting the HKEY_CLASSES_ROOT via QSettings but have not been successful (seems that the settings don't save).

Does anybody have any insight about how to do this for QT on Windows?


Solution

  • This is called URL Protocol Handler and you were following the right path of modifying the registry. HKEY_CLASSES_ROOT in fact maps to either HKEY_LOCAL_MACHINE\Software\Classes (which can have issues with write access) and HKEY_CURRENT_USER\Software\Classes, we use the latter.

    Here's the complete example that works:

    #include <QApplication>
    #include <QSettings>
    #include <QDir>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
    
        QString path = QDir::toNativeSeparators(qApp->applicationFilePath());
    
        QSettings set("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
        set.beginGroup("YourApp");
        set.setValue("Default", "URL:YourApp Protocol");
        set.setValue("DefaultIcon/Default", path);
        set.setValue("URL Protocol", "");
        set.setValue("shell/open/command/Default", QString("\"%1\"").arg(path) + " \"%1\"");
        set.endGroup();
    
        return 0;
    }