SOLVED: How do I get my app to launch the web browser?
-
I'm making apps with C++ and SDL, I've read the UBports documentation but I can't figure out how to launch the web browser from my app. On a desktop system, I use the C++ system() command and can open other programs. Example:
if ( event.type == SDL_FINGERDOWN ) {
system("sensible-browser https://forums.ubports.com/");
}It's not working on Ubuntu Touch. I've tried this:
system("morph-browser --desktop_file_hint=/usr/share/applications/morph-browser.desktop https://forums.ubports.com/");Can someone point me to some documentation on how it's done with C++?
Thank you.
-
-
@lduboeuf thank you. I downloaded the url-dispatcher package and read many of the files. I imagine I would need to run make on some of these files, put an #include in my existing main.cpp or main.h and then I can call a function that will launch the web browser? Are there any instructions for doing this? Details wont bore me, I need them.
Thank you.
-
First off, you should not use
system()
. If you must exec an external binary, you should useexec()
family of calls instead.Are you using Qt at all? Or glib/gio?
If you are using either of these, there are APIs to open URLs with the registered URL handler.
-
@dobey Thank you for your reply. Yes, with app confinement, I realized system() was not going to work.
I'm using pure C++ and SDL in my app. That's it.
I downloaded the url-dispatcher that @lduboeuf recommended.
https://github.com/ubports/url-dispatcherHowever, it didn't come with any instructions.
-
@Craig Really not sure of the url-dispatcher thing. Hope i don't gave you wrong instruction
-
@Craig
url-dispatcher
is the service which manages URL handlers on UT. It's not something you'd use directly.Try doing something like:
#include <unistd.h> ... execlp("xdg-open", MY_URL, NULL); ...
If your code is actually C++, I'd suggest using
nullptr
instead ofNULL
here, but this should do what you want. AndMY_URL
would be the URL string you want to open. Also, if using C++11 or newer, you probably would want to do theexeclp()
call inside astd::thread
as well. -
@dobey Thank you for your reply! I like your idea. It sounds simple. However, I tried it and it didn't work.
This works from adb shell:
morph-browser --desktop_file_hint=/usr/share/applications/morph-browser.desktop https://forums.ubports.com/However, from C++, this did not work:
#include <unistd.h>
...
execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );I also did "int error = execlp()..." and it always returned -1 (error).
-
@Craig said in How do I get my app to launch the web browser?:
execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );
Have you tried:
execlp("xdg-open","https://forums.ubports.com/", nullptr );
as per @dobey 's instructions?
-
@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... -
@AppLee One would need to use https://doc.qt.io/qt-5.9/qdesktopservices.html#openUrl
-
@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 useQCoreApplication
orQApplication
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 useQCoreApplication
orQApplication
instead, if possible.I think you meant something other than
QApplication
the second time you mentioned it. Which library instead ofQApplication
?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