- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个通过渲染 Jinja 模板获得的字符串。在模板中,我有 css 文件的绝对路径。例如:
<link rel='stylesheet' href="C:\Users\User\project\reports\template\css">
但是,当我在 QWebEngineView 中设置 html 时,只显示纯 HTML,没有 CSS。我怎样才能检测CSS引用?
这是我的代码
class WidgetEdificioCirsoc(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self._tab_edificio = widgets.TabEdificio()
self._webview = QtWebEngineWidgets.QWebEngineView()
layout_horizontal_principal = QtWidgets.QHBoxLayout()
layout_horizontal_principal.addWidget(self._tab_edificio)
layout_horizontal_principal.addWidget(self._webview)
self.setLayout(layout_horizontal_principal)
def calcular(self):
edificio = self._tab_edificio()
reporte = edificio.reporte.html() # Generate the string
self._webview.setHtml(reporte) # Set the string
最佳答案
QWebEngineView
当您使用 setHtml()
方法不处理 url 时,解决方法是使用 javascript 加载 css,如下所示:
.
├── main.py
└── css
└── styles.css
main.py
from PyQt5 import QtWebEngineWidgets, QtWidgets, QtCore
def loadCSS(view, path, name):
path = QtCore.QFile(path)
if not path.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
return
css = path.readAll().data().decode("utf-8")
SCRIPT = """
(function() {
css = document.createElement('style');
css.type = 'text/css';
css.id = "%s";
document.head.appendChild(css);
css.innerText = `%s`;
})()
""" % (name, css)
script = QtWebEngineWidgets.QWebEngineScript()
view.page().runJavaScript(SCRIPT, QtWebEngineWidgets.QWebEngineScript.ApplicationWorld)
script.setName(name)
script.setSourceCode(SCRIPT)
script.setInjectionPoint(QtWebEngineWidgets.QWebEngineScript.DocumentReady)
script.setRunsOnSubFrames(True)
script.setWorldId(QtWebEngineWidgets.QWebEngineScript.ApplicationWorld)
view.page().scripts().insert(script)
HTML = """
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>I am formatted with a style sheet</h1>
<p>Me too!</p>
</body>
</html>
"""
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
view.setHtml(HTML, QtCore.QUrl("index.html"))
loadCSS(view, "css/styles.css", "script1")
view.show()
sys.exit(app.exec_())
css/styles.css
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
关于python - CSS 在 QWebEngineView.setHtml() 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388443/
我正在尝试将 html 内容动态设置到主线程中的文档节点。 QWebElement dynamicContent = ui->webView->page()->main
以下代码在 FF 和 IE 中运行 TempNode= CKEDITOR.dom.element.createFromHtml(""); TempNode.setHtml("test"); 但是下面提
我正在尝试将 HTML 插入到 quill 编辑器中。 HTML 可以在样式标签中包含自定义样式。 Quill 正在用 HTML 中的 span 标签替换所有样式标签,例如这个 jsfiddle 中的
我有 FlexTable 布局 = new FlexTable();但是当我写一段文字时layout.setHTML(1, 0, "颜色是:"); 冒号出现在下一行。如何将整个文本限制为一行。有人可以
我有一个通过渲染 Jinja 模板获得的字符串。在模板中,我有 css 文件的绝对路径。例如: 但是,当我在 QWebEngineView 中设置 html 时,只显示纯 HTML,没有 CSS。我
我正在尝试将 HTML 插入到一个简单的 div 中。 HTML 没有进入 div,但如果我在 setHTML 调用之前直接提醒它,那么它就完美了。该代码在 setHTML 调用上失败。这是 Moot
我尝试使用 CSS 和 HTML 来对齐我的文本,但它们都不起作用。 HTML 对齐属性以某种方式适用于 h1 和 h2 但不适用于 div 或 span。 QTextDocument doc; do
每当我尝试通过以下方式加载页面时: QWebView *view = new QWebView(); view->settings()->setAttribute(QWebSettings::Auto
我正在使用 SendGrid( https://sendgrid.com/ ) 从我的应用程序发送电子邮件。我对 SendGrid SMTP API 的使用有疑问。 我有 SendGrid 模板 my
我尝试了两种不同的方式在 PyQt 中加载和显示 SVG 文件: import sys from PyQt5.QtWebEngineWidgets import QWebEngineView from
我有以下 certificate 类,用于从一些图像和数据中生成 pdf 文档。设置图像源后,我调用 generate() 函数并获取 test.pdf 输出文件。该文档是基于 QTextDocume
我用 QTreeView 和 QWebEngineView 创建了简单的 Qt 窗口布局:在 TreeView 中选择一些项目后,Web 引擎 View 会显示一些内容。问题是当调用 QWebEngi
我在使用以下示例时遇到问题: mapboxgl.accessToken = 'pk.eyJ1IjoicGFwYWJ1Y2t0IiwiYSI6ImNqa2k3azQ1dzA1Zmgza3B1czIxOG
我正在使用 IsDirty 来检查 FCKeditor 中的更改。不幸的是,它的功能似乎是异步的。 这是失败的代码: var txtObj = $('activities').EstActText1.
我想知道是否可以在 mapboxgl.Popup() 中渲染 React 组件。像这样的事情: componentDidMount() { new mapboxgl.P
打开一个只有一个 FCKeditor 窗口的网页后,我得到了实例: i = FCKeditorAPI.GetInstance( "txtText" ) 这行得通。我也被允许: i.GetHTML()
我是一名优秀的程序员,十分优秀!