Qt WebEngineView:将自定义字体设置为html内容
如何将自定义字体(字体在qrc中)应用于Qt WebEngineView:将自定义字体设置为html内容,qt,qml,Qt,Qml,如何将自定义字体(字体在qrc中)应用于WebEngineView中的html内容的某些部分?我知道了如何使用自定义字体(但我不确定这是否安全): 向qrc添加自定义字体:://res/font/myfont.ttf 使用以下内容在qrc中创建style.css: @font-face { font-family: myfont; src: url('qrc:/res/font/myfont.ttf'); font-weight: bold; } body { font-f
WebEngineView
中的html内容的某些部分?我知道了如何使用自定义字体(但我不确定这是否安全):
://res/font/myfont.ttf
李>
style.css
:
@font-face {
font-family: myfont;
src: url('qrc:/res/font/myfont.ttf');
font-weight: bold;
}
body {
font-family: 'myfont';
text-align: justify;
line-height: 25px;
}
WebEngineView
时,将style.css
添加到HTML:
WebEngineView {
id: webEngineView
onLoadingChanged: {
if (loadRequest.status == WebEngineView.LoadSucceededStatus) {
webEngineView.runJavaScript(
"var headHTML = document.documentElement.getElementsByTagName('head')[0].innerHTML;" +
"headHTML += '<link rel=\"stylesheet\" href=\"qrc:/css/styles.css\">';" +
"document.documentElement.getElementsByTagName('head')[0].innerHTML = headHTML;"
);
}
}
}
WebEngineView{
id:webEngineView
onLoadingChanged:{
如果(loadRequest.status==WebEngineView.LoadSucceededStatus){
webEngineView.runJavaScript(
“var headHTML=document.documentElement.getElementsByTagName('head')[0]。innerHTML;”+
“headHTML+=”;“+
“document.documentElement.getElementsByTagName('head')[0]。innerHTML=headHTML;”
);
}
}
}
--禁用web安全性
因为这是
*视图
,所以您不能编辑内容,只能查看。或者,作为一种解决方法,您可以获取页面html代码,通过调用WebEngineView.loadHtml()
html内容加载WebEngineView.loadHtml()
,对其进行更改和设置。我可以像这样使用QML中的自定义字体:TextArea{font.family:“myFont”}
。它正在工作。问题是如何在WebEngineView中使用CSS从HTML内容使用此字体?