Skip to content

Instantly share code, notes, and snippets.

@oglops
Created June 27, 2017 06:42
Show Gist options
  • Save oglops/39f25c1309c167955ad27e114550fcb0 to your computer and use it in GitHub Desktop.
Save oglops/39f25c1309c167955ad27e114550fcb0 to your computer and use it in GitHub Desktop.
Load one or multiple css files into qwebview
import sys
from PyQt4.QtWebKit import QWebView, QWebInspector, QWebSettings
from PyQt4.QtGui import QApplication, QDialog, QHBoxLayout
from PyQt4.QtCore import QUrl, QByteArray, QString
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
layout = QHBoxLayout()
self.browser = QWebView()
self.browser.page().settings().setAttribute(
QWebSettings.DeveloperExtrasEnabled, True)
self.inspector = QWebInspector(self)
self.inspector.setPage(self.browser.page())
layout.addWidget(self.browser)
layout.addWidget(self.inspector)
self.setLayout(layout)
self.browser.load(QUrl('video.html'))
self.resize(1800, 800)
self.browser.loadFinished.connect(self.loadCSS)
def loadCSS(self):
# method 1 global css
# QWebSettings.globalSettings().setUserStyleSheetUrl(QUrl.fromLocalFile('/home/oglop/qt5_test/video.css'))
# method 2 combining mutile css files
files = [
'video_1.css',
'video_2.css'
]
cssUri = 'data:text/css;charset=utf-8;base64,%s'
css = ''
for f in files:
content = open(f).read()
css += content
cssUri = cssUri % QByteArray(css).toBase64()
self.browser.settings().setUserStyleSheetUrl(
QUrl(QString.fromUtf8(cssUri)))
# other methods https://forum.qt.io/topic/70296/how-to-load-multiple-css-files-in-qwebview/2
# Merge all your CSS into one file;
# Create a "general" one and use @import url("css1.css"); for every CSS you want in it;
# Add them from HTML with <link rel="stylesheet" type="text/css"
# href="path2css.css"/>? I don't know if it's possible within QWebView.
app = QApplication(sys.argv)
dialog = Dialog()
dialog.show()
app.exec_()
audio::-webkit-media-controls, video::-webkit-media-controls {
opacity: 0;
-webkit-transition: opacity 0.3s ease;
}
audio::-webkit-media-controls:hover, video::-webkit-media-controls:hover {
opacity: 1;
-webkit-transition: opacity 0.3s ease;
}
video::-webkit-media-controls-current-time-display {
color: #66ccff;
}
audio::-webkit-media-controls-panel, video::-webkit-media-controls-panel {
background-color: rgba(20, 20, 20, 0.5);
}
video {
padding: 2px;
border: 4px solid #cccccc;
}
<!DOCTYPE html>
<html>
<head>
<title>qwebview test</title>
</head>
<body>
<video width="640" height="480" controls loop>
<source src="/home/oglop/qt5_test/gimp.ogv" type="video/ogg">
</video>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment