Make testbrowser.py run without QtWebKit

This commit is contained in:
Florian Bruhin 2016-09-05 18:50:39 +02:00
parent 7592345b6e
commit c503c2953d

View File

@ -25,14 +25,16 @@ import argparse
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKit import QWebSettings
try:
from PyQt5.QtWebKitWidgets import QWebView from PyQt5.QtWebKitWidgets import QWebView
except ImportError:
QWebView = None
try: try:
from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtWebEngineWidgets import QWebEngineView
WEBENGINE = True
except ImportError: except ImportError:
WEBENGINE = False QWebEngineView = None
def parse_args(): def parse_args():
@ -41,7 +43,7 @@ def parse_args():
parser.add_argument('url', help='The URL to open') parser.add_argument('url', help='The URL to open')
parser.add_argument('--plugins', '-p', help='Enable plugins', parser.add_argument('--plugins', '-p', help='Enable plugins',
default=False, action='store_true') default=False, action='store_true')
if WEBENGINE: if QWebEngineView is not None:
parser.add_argument('--webengine', help='Use QtWebEngine', parser.add_argument('--webengine', help='Use QtWebEngine',
default=False, action='store_true') default=False, action='store_true')
return parser.parse_args() return parser.parse_args()
@ -51,16 +53,29 @@ if __name__ == '__main__':
args = parse_args() args = parse_args()
app = QApplication(sys.argv) app = QApplication(sys.argv)
if WEBENGINE and args.webengine: if QWebView is None and QWebEngineView is None:
print("Found no suitable backend to run with!")
sys.exit(1)
elif QWebView is None and not args.webengine:
print("Using QtWebEngine because QtWebKit is unavailable")
wv = QWebEngineView() wv = QWebEngineView()
using_webengine = True
elif args.webengine:
if QWebEngineView is None:
print("Requested QtWebEngine, but it could not be imported!")
sys.exit(1)
wv = QWebEngineView()
using_webengine = True
else: else:
wv = QWebView() wv = QWebView()
using_webengine = False
wv.loadStarted.connect(lambda: print("Loading started")) wv.loadStarted.connect(lambda: print("Loading started"))
wv.loadProgress.connect(lambda p: print("Loading progress: {}%".format(p))) wv.loadProgress.connect(lambda p: print("Loading progress: {}%".format(p)))
wv.loadFinished.connect(lambda: print("Loading finished")) wv.loadFinished.connect(lambda: print("Loading finished"))
if args.plugins and not WEBENGINE: if args.plugins and not using_webengine:
from PyQt5.QtWebKit import QWebSettings
wv.settings().setAttribute(QWebSettings.PluginsEnabled, True) wv.settings().setAttribute(QWebSettings.PluginsEnabled, True)
wv.load(QUrl.fromUserInput(args.url)) wv.load(QUrl.fromUserInput(args.url))