Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
Migrate deprecated PyQt 5.5 to new PyQt 5.10
Browse files Browse the repository at this point in the history
Migrate from WebKit to WebEngine
  • Loading branch information
ELC committed Feb 22, 2018
1 parent c44a019 commit ef6f858
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys
import webbrowser

from PyQt5 import QtCore, QtWidgets, QtWebKitWidgets, QtGui
from PyQt5 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets


def init_gui(application, port=5000, width=300, height=400,
Expand All @@ -10,37 +8,43 @@ def init_gui(application, port=5000, width=300, height=400,
ROOT_URL = 'http://localhost:{}'.format(port)

# open links in browser from http://stackoverflow.com/a/3188942/1103397 :D
def link_clicked(url):
# thanks to https://github.com/marczellm/qhangups/blob/cfed73ee4383caed1568c0183a9906180f01cb00/qhangups/WebEnginePage.py
def link_clicked(url, typ, ismainframe):
ready_url = url.toEncoded().data().decode()
if ROOT_URL not in ready_url:
webbrowser.open(ready_url)
else:
window.webView.load(QtCore.QUrl(ready_url))
is_clicked = typ == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked
is_not_internal = ROOT_URL not in ready_url
if is_clicked and is_not_internal:
QtGui.QDesktopServices.openUrl(url)
return False
return True

def run_app():
application.run(port=port, threaded=True)

# Application Level
qtapp = QtWidgets.QApplication(sys.argv)

webapp = QtCore.QThread()
webapp.__del__ = webapp.wait
webapp.run = run_app
webapp.start()

qtapp.aboutToQuit.connect(webapp.terminate)

# Main Window Level
window = QtWidgets.QMainWindow()
window.resize(width, height)
window.setWindowTitle(window_title)
window.webView = QtWebKitWidgets.QWebView(window)
window.setCentralWidget(window.webView)
window.setWindowIcon(QtGui.QIcon(icon))

window.webView.page().setLinkDelegationPolicy(
QtWebKitWidgets.QWebPage.DelegateAllLinks)
window.webView.page().linkClicked.connect(link_clicked)
# WebView Level
window.webView = QtWebEngineWidgets.QWebEngineView(window)
window.setCentralWidget(window.webView)

# WebPage Level
page = QtWebEngineWidgets.QWebEnginePage()
page.acceptNavigationRequest = link_clicked
page.load(QtCore.QUrl(ROOT_URL))
window.webView.setPage(page)

window.webView.load(QtCore.QUrl(ROOT_URL))
window.show()

return qtapp.exec_()

0 comments on commit ef6f858

Please sign in to comment.