SOLVED: How do I get my app to launch the web browser?



  • @arubislander Yes, I did try that first. It appears xdg-open isn't installed on Ubuntu Touch. Have you tried it from a C++ app? If you try it from adb shell it says, "bash: xdg-open: command not found".



  • I must admit I haven't. Just checked, and indeed xdg-open is not installed. So the question becomes: What are the options for launching the default url handler?



  • There is a thread somewhere on this forum concerning launching the browser from QML. I do not have the opportunity to search for it a.t.m. Might do so when I get home, if no one beats me to it. I am thinking something similar must be posible from C++ 🤔



  • From QML, I have this working :

     Action {
            iconName: "info"
            onTriggered: Qt.openUrlExternally("https://www.domoticz.com/")
          }
    

    I don't remember what to include to have Qt.openUrlExternally() in your C++ code.
    Might help...





  • @dobey You are right. I was testing QDesktopServices::openUrl when you posted this.

    My C++/SDL app launches the morph web browser. Here's how to make a C++/SDL app launch the web browser in Ubuntu Touch:

    Step 1:
    You need to add these to your CMakeLists.txt:
    (You may have more than one CMakeList.txt file and some of the code lines below will go in different CMakeList.txt files.)

    (Add these lines below after "project(...)" and "cmake_minimum_required(..)")

    find_package(Qt5Core)
    find_package(Qt5Qml)
    find_package(Qt5Quick)
    

    (Add this line below after "execute_process(... OUTPUT_VARIABLE ARCH_TRIPLET ...)" )

    set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")
    

    (Add this line below after "add_executable(... main.cpp ...)" )

    qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)
    

    Replace YOURPROJECTNAMEHERE with your project name.

    Step 2:
    Put these headers near the top of main.h/main.cpp:

    #include <QGuiApplication>
    #include <QDesktopServices>
    #include <QUrl>
    

    Put this at the top of main():

    QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
    

    Near the end of main(), replace "return 0;" with this:

    return app->exec();
    

    Step 3:
    And now for the magic... Somewhere in your C++ code, make your link:

    if ( event.type == SDL_FINGERDOWN )
    {
        QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
    }
    

    If you have any comments, suggestions or ways to improve this code, let me know. Thank you all for your help.



  • @Craig said in How do I get my app to launch the web browser?:

    find_package(Qt5Core)
    find_package(Qt5Qml)
    find_package(Qt5Quick)

    You don't need Quick or Qml here. Though I think you will need Gui. Also it'd be better to do:

    find_package(Qt5 COMPONENTS Core Gui)
    

    set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")

    You don't need this, as you aren't using any QML parts.

    qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)

    For this, you should instead just add

    Qt5::Core
    Qt5::Gui
    

    to the list of libraries you pass to the target_link_libraries() call.

    I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.



  • @dobey I made the simplifications to the CMakeLists.txt file that you suggested. It's working, thank you!

    In the last part of your previous message, you wrote:

    I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.

    I think you meant something other than QApplication the second time you mentioned it. Which library instead of QApplication?

    I tried it with these in main():

    QCoreApplication a(argc, args);
    return a.exec();
    

    Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.



  • @Craig said in How do I get my app to launch the web browser?:

    Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.

    OK. I suppose you do need to use QGuiApplication then.



  • So, just to recap, here's how to make a C++ app launch the web browser in Ubuntu Touch:

    Step 1:
    You need to add/change these in your CMakeLists.txt:

    find_package(Qt5 COMPONENTS Core Gui)
    target_link_libraries(YOURPROJECTNAMEHERE ... Qt5::Core Qt5::Gui)
    

    Step 2:
    Put these headers near the top of main.h/main.cpp:

    #include <QGuiApplication>
    #include <QDesktopServices>
    #include <QUrl>
    

    Put this at the top of main():

    QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
    

    Near the end of main(), replace "return 0;" with this:

    return app->exec();
    

    Step 3:
    Somewhere in your C++ code, when the app user clicks on your button, call this:

    QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
    


  • @Craig thanks for sharing 🙂


Log in to reply