Skip to content

Qt5 backend#1329

Open
snuk182 wants to merge 23 commits intowebview:masterfrom
snuk182:qt5_navigate
Open

Qt5 backend#1329
snuk182 wants to merge 23 commits intowebview:masterfrom
snuk182:qt5_navigate

Conversation

@snuk182
Copy link
Copy Markdown

@snuk182 snuk182 commented Sep 12, 2025

Just in case someone needs it, a working Qt5 backend.
I am not a C++ developer, so the code can contain flaws. Please feel free to criticize or discard.
I used Qt5, and not Qt6 (now supported as well) , because my own projects is yet based on version 5.

Since the whole project targets CMake 3.16, some of the directives (-fPIC) are provided as raw compiler flags, not the CMake properties (these did not work for me).

plyhun and others added 17 commits April 17, 2025 15:38
Removes some details about expected behavior of webview_set_size() due
to inconsistencies observed when using GTK 4.

The documentation states the following:

    GTK 4 can set a default/initial window size if done early enough;
    otherwise, this function has no effect. GTK 4 (unlike 3) can't
    resize a window after it has been set up.

Observations when calling webview_set_size() with WEBVIEW_HINT_NONE:

Ubuntu  | GTK     | Window  | Can set new size?
--      | ---     | ------- | -----------------
24.04.2 | 4.14.2  | X11     | If different from what was previously set
24.04.2 | 4.14.2  | Wayland | If user hasn't resized by hand
24.04.2 | 3.24.41 | X11     | Yes
24.04.2 | 3.24.41 | Wayland | Yes
22.04.5 | 4.6.9   | X11     | If different from what was previously set
22.04.5 | 4.6.9   | Wayland | Yes
22.04.5 | 3.24.33 | X11     | Yes
22.04.5 | 3.24.33 | Wayland | Yes
gtk_widget_set_size_request() was called when WEBVIEW_HINT_FIXED was
passed into webview_set_size(). That function is meant to set a minimum
size.

Instead we can call gtk_window_set_default_size() (GTK 4) or
gtk_window_resize() (GTK 3).
@IriaSomobu
Copy link
Copy Markdown

I've faced the following problem:

Loading file: file:///home/somobu/[some-path]/index.html
js: Access to XMLHttpRequest at 'https://[some-domain]/[some-endpoint]' from origin 'file://' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

It can be solved by adding to the core/include/webview/detail/backends/qt5_qwebengine.hh, function void window_settings(bool debug) the following line:

m_webview->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);

Not sure if it is 100% safe to do, but at least it works for me.

@IriaSomobu
Copy link
Copy Markdown

Also, to prevent gcc from complaining that _deps/webview-src/core/include/webview/detail/backends/../platform/linux/gtk/compat.hh:35:10: fatal error: gtk/gtk.h: No such file or directory, I had to set both set(WEBVIEW_QT true) and add_definitions(-DWEBVIEW_QT) in my project's CMakeLists.txt (setting only one of them isn't enough). Not sure if this was intended.

@IriaSomobu
Copy link
Copy Markdown

It looks like you cannot make window resizeable. The following line makes window resizable on GTK webview, but not on Qt5 one:

w.set_size(1280, 800, WEBVIEW_HINT_NONE);

@IriaSomobu
Copy link
Copy Markdown

Qt5 has reached its EOL, and therefore qt5-webengine has been removed from Arch repos one month ago.

@snuk182
Copy link
Copy Markdown
Author

snuk182 commented Dec 10, 2025

Also, to prevent gcc from complaining that _deps/webview-src/core/include/webview/detail/backends/../platform/linux/gtk/compat.hh:35:10: fatal error: gtk/gtk.h: No such file or directory, I had to set both set(WEBVIEW_QT true) and add_definitions(-DWEBVIEW_QT) in my project's CMakeLists.txt (setting only one of them isn't enough). Not sure if this was intended.

Did some research and found https://discourse.cmake.org/t/cmake-command-line-d-and-using-defined/2264/6 :

The command line interface explicitly considers -DVAR an error.

I use -DWEBVIEW_QT=1 when making test builds from command line, as -DWEBVIEW_QT makes me the same troubles.

@snuk182
Copy link
Copy Markdown
Author

snuk182 commented Dec 10, 2025

Qt5 has reached its EOL

Ugh. Will need some time to upgrade...

@IriaSomobu
Copy link
Copy Markdown

Ugh. Will need some time to upgrade...

File cmake/webview.cmake:

  • replace all Qt5 -> Qt6;
  • remove Qt5WebEngine (it seems that this pkg doen't exist anymore);

File core/CMakeLists.txt:

  • replace Qt5 -> Qt6;

File core/include/webview/detail/backends/qt5_qwebengine.hh:

  • m_webview->page()->scripts().findScripts(script_name); -> ...->script().find(script_name);;
  • in m_app = new QApplication(m_argc, m_argv); constructor call you now have to provide non-zero argc -- first argument is an app name (in practice it can be any non-zero string, looks like Qt does nothing with it);

Transition to Qt6 introduces regression: webview data is now lost on app restart.

@IriaSomobu
Copy link
Copy Markdown

To make webview storage persistent, you have to make the following changes in core/include/webview/detail/backends/qt5_qwebengine.hh:

  1. Add QWebEngineProfile* prof field on a qt_web_engine;

  2. In qt_web_engine::window_init, initialize the profile:

// Initialize webview widget
prof = new QWebEngineProfile(QString("Default"), nullptr);
m_webview = new QWebEngineView(prof, nullptr);
  1. In qt_web_engine::~qt_web_engine, destroy the profile (data won't be saved if you not call profile destructor);

@snuk182
Copy link
Copy Markdown
Author

snuk182 commented Feb 2, 2026

Took a while for personal reasons. Qt version is being switched by WEBVIEW_QT value, 5 stands for 5, everything else is for 6.

@peterold-eth
Copy link
Copy Markdown

I love you so much. That was my fault. I', really sorry)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

8 participants