Results 1 to 6 of 6

Thread: Url protocol scheme

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Url protocol scheme

    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

    Qt Code:
    1. bool Settings::registerProtocol()
    2. {
    3. #ifdef Q_OS_WIN
    4. const QString urlScheme = "myapp";
    5. const QString appPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
    6. const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes\\") + urlScheme;
    7.  
    8. QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));
    9.  
    10. reg->setValue(QStringLiteral("Default"), "download manager");
    11. reg->setValue(QStringLiteral("URL Protocol"), QString());
    12.  
    13. reg->beginGroup(QStringLiteral("DefaultIcon"));
    14. reg->setValue(QStringLiteral("Default"), QString("%1,1").arg(appPath));
    15. reg->endGroup();
    16.  
    17. reg->beginGroup(QStringLiteral("shell"));
    18. reg->beginGroup(QStringLiteral("open"));
    19. reg->beginGroup(QStringLiteral("command"));
    20. reg->setValue(QStringLiteral("Default"), appPath + QLatin1String(" %1"));
    21.  
    22. return true;
    23.  
    24. #elif defined(Q_OS_UNIX)
    25. //TODO
    26. Logger::getInstance()->Info(tr("Cannot integrate with web browser - unsupported system"));
    27. return false;
    28. #endif
    29. return false;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Receiving messages

    Qt Code:
    1. void MainWindow::checkApplicationArguments()
    2. {
    3. for(int i = 0; i < QCoreApplication::arguments().count(); i++)
    4. {
    5. QString arg = QCoreApplication::arguments().at(i);
    6. if(arg.contains("myapp:["))
    7. {
    8. //do something
    9. }
    10. }
    11. }
    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:
    Qt Code:
    1. 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

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Url protocol scheme

    I believe you will have to subclass QNetworkAccessManager to implement your own protocol and then see the doc for QNetworkAccessManager::supportedSchemesImplementation().
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Url protocol scheme

    You are looking for something like a single instance application.
    E.g. http://stackoverflow.com/questions/5...app-protection

    Cheers,
    _

  4. #4
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Url protocol scheme

    I've got single instance app protection using QSharedMemory

    I've attached example app

    http://www79.zippyshare.com/v/gW9uCi3f/file.html

    Please open it, and click button "Enable integration with web browsers", then close app and enter myapp:[hello] into web browser address bar.
    App will start and you will see received message, but how can I get message when app is already running ?

    Cheers
    Last edited by #Dragon; 15th January 2016 at 12:06.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Url protocol scheme

    Quote Originally Posted by #Dragon View Post
    App will start and you will see received message, but how can I get message when app is already running ?
    That's what single instance mechanisms usually do.
    They detect that there is an already running instance and then pass the argument to the running instance.

    Since you implement your own single instance mechanism, it is also up to you to implement instance communication.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    #Dragon (15th January 2016)

  7. #6
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Url protocol scheme

    Thank You again, all is done and now works properly

Similar Threads

  1. Getting Device Interface Protocol (SCSI or ATA protocol)
    By Ryuuji in forum General Programming
    Replies: 1
    Last Post: 18th July 2013, 09:37
  2. SSL protocol
    By tpthyd in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2009, 13:53
  3. Protocol Implementation. ?
    By dheeraj in forum Qt Programming
    Replies: 32
    Last Post: 9th May 2008, 11:26
  4. Modbus Protocol
    By ^NyAw^ in forum General Programming
    Replies: 0
    Last Post: 21st January 2008, 11:37
  5. Signalling Protocol
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 7
    Last Post: 16th February 2006, 06:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.