Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 525
Anthony Dito Avatar asked Oct 20 '25 18:10

Anthony Dito


1 Answers

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;
}
like image 80
Zoin Avatar answered Oct 23 '25 07:10

Zoin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!