Hi, last time I am trying to implement a Url protocol scheme, How it works ? Very simple, as first we need to register a url scheme in our system, to assign url address with our app and then we can send some message using web browser address bar.
Code to register url address
bool Settings::registerProtocol()
{
#ifdef Q_OS_WIN
const QString regPath
= QStringLiteral
("HKEY_CURRENT_USER\\Software\\Classes\\") + urlScheme;
reg->setValue(QStringLiteral("Default"), "download manager");
reg
->setValue
(QStringLiteral
("URL Protocol"),
QString());
reg->beginGroup(QStringLiteral("DefaultIcon"));
reg
->setValue
(QStringLiteral
("Default"),
QString("%1,1").
arg(appPath
));
reg->endGroup();
reg->beginGroup(QStringLiteral("shell"));
reg->beginGroup(QStringLiteral("open"));
reg->beginGroup(QStringLiteral("command"));
reg
->setValue
(QStringLiteral
("Default"), appPath
+ QLatin1String(" %1"));
return true;
#elif defined(Q_OS_UNIX)
//TODO
Logger::getInstance()->Info(tr("Cannot integrate with web browser - unsupported system"));
return false;
#endif
return false;
}
bool Settings::registerProtocol()
{
#ifdef Q_OS_WIN
const QString urlScheme = "myapp";
const QString appPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes\\") + urlScheme;
QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));
reg->setValue(QStringLiteral("Default"), "download manager");
reg->setValue(QStringLiteral("URL Protocol"), QString());
reg->beginGroup(QStringLiteral("DefaultIcon"));
reg->setValue(QStringLiteral("Default"), QString("%1,1").arg(appPath));
reg->endGroup();
reg->beginGroup(QStringLiteral("shell"));
reg->beginGroup(QStringLiteral("open"));
reg->beginGroup(QStringLiteral("command"));
reg->setValue(QStringLiteral("Default"), appPath + QLatin1String(" %1"));
return true;
#elif defined(Q_OS_UNIX)
//TODO
Logger::getInstance()->Info(tr("Cannot integrate with web browser - unsupported system"));
return false;
#endif
return false;
}
To copy to clipboard, switch view to plain text mode
Receiving messages
void MainWindow::checkApplicationArguments()
{
{
if(arg.contains("myapp:["))
{
//do something
}
}
}
void MainWindow::checkApplicationArguments()
{
for(int i = 0; i < QCoreApplication::arguments().count(); i++)
{
QString arg = QCoreApplication::arguments().at(i);
if(arg.contains("myapp:["))
{
//do something
}
}
}
To copy to clipboard, switch view to plain text mode
... and then if we will register it, we can enter a message into web browser address bar and send it to our app, for example:
myapp:[hello]
myapp:[hello]
To copy to clipboard, switch view to plain text mode
then program will start and we can receive our message, but I'm wondering and I have completely no idea how to receive a message when app is currently running... normally web browser tries to open a new instance of application...
Have you got some idea how to handle application args when program is open ? I know that this is possible, I have seen it in different app about two years ago but the project is no longer being developed and I can't remember how it worked...
Thank you for time spent reading this topic
Cheers
Bookmarks