I originally want to solve this problem , but as I have tested the behavior of QWebEnginePage::runJavaScript() , I found I cannot even change the backgroundImage using QWebEnginePage::runJavaScript() with the following code, so why ?
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
class WebEngineView(QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
self.webPage = self.page()
# self.webPage = WebEnginePage()
self.webPage.load(QUrl('https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style'))
self.webPage.runJavaScript('''
window.addEventListener("load", function(event) {
alert(document.title);
document.body.style.backgroundImage = "url('https://www.w3schools.com/jsref/img_tree.png')";
});
''') # QWebEngineScript.MainWorld
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_UseSoftwareOpenGL)
webEngineView = WebEngineView()
webEngineView.show()
sys.exit(app.exec_())