QWebView::settings()->setUserStyleSheetUrl() 从 QtWebKit 到 QtWebEngine?

QWebView::settings()->setUserStyleSheetUrl() from QtWebKit to QtWebEngine?

我正在将我的代码从 Qt 5.5 升级到 Qt 5.6,但我没有找到移植以下代码的方法:

QWebEngineView *qwebview = new QWebEngineView(this);
qwebview->settings()->setUserStyleSheetUrl(QUrl("qrc:/about.css"));
qwebview->setHtml(fileContentStr);

使用新的 Qt Web 引擎 CSS 插入用户的最佳方式是什么?

如果您阅读此处:Bug Report on QWebEngine,您会看到:

The only opportunity to inject CSS code into the WebEngineView is using JavaScript. It would be nice to have an appropriate API for this and it could look like our already existing UserScripts API.

看起来很清楚:您不能再使用 WebSettings 来插入 CSS。 相反,您将不得不使用 HTML/JavaScript 来做那。

我不知道它是否对你有帮助,但这里是我所做的摘录。

在我的 .cpp 中:

m_pView = new QWebEngineView(this);

QUrl startURL = QUrl("path/to/index.html");

m_pView->setUrl(startURL);

并且在 index.html 中:

<html>
    <head>
        <title>TITLE</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <!-- Many JS scripts here -->

        <link href="./css/style.css" rel="stylesheet">

    </head>
    <body ng-app="myApp" >
      <div ui-view class="page"></div>
    </body>
</html>

希望对您有所帮助。