最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Qt Quick 6.8 WebEngine - Display local HTML file - Stack Overflow

programmeradmin5浏览0评论

I have a Qt Quick application running on C++ and I wanted to add advanced graphics features using the QtWebEngine. This engine is based on Chomium and everything was going fine so far. I am able to open the chromium browser inside my Qt Quick app, and access any website.

The issue is that I want to display a local html file, and I always get this error : [5524:22372:0209/133920.794:ERROR:html_media_element(4888)] SetError: {code=4, message="MEDIA_ELEMENT_ERROR: Media load rejected by URL safety check"}

Here is what I did :

1 - I added my HTML file to the SOURCES section of my CMakeLists.txt :

qt_add_qml_module(appSingerStudio
    URI SingerStudio
    VERSION 1.0
    RESOURCES
        qt-logo.png
    SOURCES
        ProjectManager.h
        SettingsManager.h
        html/index.html
        html/script.js
        html/styles.css
    QML_FILES
        Main.qml
        Page.qml
        Home.qml
        ProjectPage.qml
        ProjectCreation.qml
        AudioPlayer.qml
        LyricsEditor.qml
        EditWebView.qml
)

2 - I changed the settings configuration of my default chromium profile in my main.cpp#

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtWebEngineQuick/qtwebenginequickglobal.h>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QDebug>

#include "ProjectManager.h"
#include "SettingsManager.h"

int main(int argc, char *argv[])
{
    // Créer l'objet QApplication ou QGuiApplication en premier
    QGuiApplication app(argc, argv);

    // Initialiser WebEngine
    
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true);
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::AllowGeolocationOnInsecureOrigins, true);
    QtWebEngineQuick::initialize(); //I tried to initialize before and after but it doesn't matter

    QQmlApplicationEngine engine;

    SettingsManager settingsManager;
    settingsManager.loadSettings();
    ProjectManager projectManager(&settingsManager);

    qmlRegisterSingletonInstance("com.example.settings", 1, 0, "SettingsManager", &settingsManager);
    engine.rootContext()->setContextProperty("projectManager", &projectManager);
    engine.rootContext()->setContextProperty("settingsManager", &settingsManager);

    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);

    engine.loadFromModule("SingerStudio", "Main");

    // Lancer l'application
    return app.exec();
}

3 - I configured the URL in my .qml component :#

import QtQuick
import QtWebEngine

WebEngineView {
    anchors.fill: parent
    url: "file:///" + Qt.resolvedUrl("html/index.html")
    Component.onCompleted: {
       console.log("URL loaded:", url);
   }
}

I also tried to simply use "html/index.html" url. The error disapears but I have a blank page.

I am just starting with Qt Quick and I couldn't find any helpful content on the Web. I would be extremly grateful if you could help me !

发布评论

评论列表(0)

  1. 暂无评论