c – 从QtWebKit到QtWebEngine的QWebView :: settings() – > setUserStyleSheetUrl()?

我正在将我的代码从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>

希望有所帮助.

点赞