Created
March 10, 2012 23:53
-
-
Save iuridiniz/2014080 to your computer and use it in GitHub Desktop.
WebKit in pyQT - http://www.rkblog.rk.edu.pl/w/p/webkit-pyqt-rendering-web-pages/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Form implementation generated from reading ui file 'httpWidget.ui' | |
# | |
# Created: Sat Dec 6 17:47:32 2008 | |
# by: PyQt4 UI code generator 4.4.4 | |
# | |
# WARNING! All changes made in this file will be lost! | |
from PyQt4 import QtCore, QtGui | |
class Ui_HttpWidget(object): | |
def setupUi(self, HttpWidget): | |
HttpWidget.setObjectName("HttpWidget") | |
HttpWidget.resize(636, 336) | |
self.verticalLayout = QtGui.QVBoxLayout(HttpWidget) | |
self.verticalLayout.setObjectName("verticalLayout") | |
self.horizontalLayout = QtGui.QHBoxLayout() | |
self.horizontalLayout.setObjectName("horizontalLayout") | |
self.back = QtGui.QPushButton(HttpWidget) | |
icon = QtGui.QIcon() | |
icon.addPixmap(QtGui.QPixmap("back.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) | |
self.back.setIcon(icon) | |
self.back.setObjectName("back") | |
self.horizontalLayout.addWidget(self.back) | |
self.next = QtGui.QPushButton(HttpWidget) | |
self.next.setEnabled(True) | |
self.next.setLayoutDirection(QtCore.Qt.RightToLeft) | |
icon1 = QtGui.QIcon() | |
icon1.addPixmap(QtGui.QPixmap("next.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) | |
self.next.setIcon(icon1) | |
self.next.setObjectName("next") | |
self.horizontalLayout.addWidget(self.next) | |
self.stop = QtGui.QPushButton(HttpWidget) | |
icon2 = QtGui.QIcon() | |
icon2.addPixmap(QtGui.QPixmap("stop.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) | |
self.stop.setIcon(icon2) | |
self.stop.setObjectName("stop") | |
self.horizontalLayout.addWidget(self.stop) | |
self.reload = QtGui.QPushButton(HttpWidget) | |
icon3 = QtGui.QIcon() | |
icon3.addPixmap(QtGui.QPixmap("reload.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) | |
self.reload.setIcon(icon3) | |
self.reload.setObjectName("reload") | |
self.horizontalLayout.addWidget(self.reload) | |
self.url = QtGui.QLineEdit(HttpWidget) | |
self.url.setObjectName("url") | |
self.horizontalLayout.addWidget(self.url) | |
self.verticalLayout.addLayout(self.horizontalLayout) | |
self.webView = QtWebKit.QWebView(HttpWidget) | |
self.webView.setUrl(QtCore.QUrl("about:blank")) | |
self.webView.setObjectName("webView") | |
self.verticalLayout.addWidget(self.webView) | |
self.retranslateUi(HttpWidget) | |
QtCore.QMetaObject.connectSlotsByName(HttpWidget) | |
def retranslateUi(self, HttpWidget): | |
HttpWidget.setWindowTitle(QtGui.QApplication.translate("HttpWidget", "Form", None, QtGui.QApplication.UnicodeUTF8)) | |
self.back.setToolTip(QtGui.QApplication.translate("HttpWidget", "Back", None, QtGui.QApplication.UnicodeUTF8)) | |
self.back.setText(QtGui.QApplication.translate("HttpWidget", "Back", None, QtGui.QApplication.UnicodeUTF8)) | |
self.next.setToolTip(QtGui.QApplication.translate("HttpWidget", "Next", None, QtGui.QApplication.UnicodeUTF8)) | |
self.next.setText(QtGui.QApplication.translate("HttpWidget", "Next", None, QtGui.QApplication.UnicodeUTF8)) | |
self.stop.setToolTip(QtGui.QApplication.translate("HttpWidget", "Stop", None, QtGui.QApplication.UnicodeUTF8)) | |
self.stop.setText(QtGui.QApplication.translate("HttpWidget", "Stop", None, QtGui.QApplication.UnicodeUTF8)) | |
self.reload.setToolTip(QtGui.QApplication.translate("HttpWidget", "Reload", None, QtGui.QApplication.UnicodeUTF8)) | |
self.reload.setText(QtGui.QApplication.translate("HttpWidget", "Reload", None, QtGui.QApplication.UnicodeUTF8)) | |
from PyQt4 import QtWebKit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ui version="4.0" > | |
<class>HttpWidget</class> | |
<widget class="QWidget" name="HttpWidget" > | |
<property name="geometry" > | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>636</width> | |
<height>336</height> | |
</rect> | |
</property> | |
<property name="windowTitle" > | |
<string>Form</string> | |
</property> | |
<layout class="QVBoxLayout" name="verticalLayout" > | |
<item> | |
<layout class="QHBoxLayout" name="horizontalLayout" > | |
<item> | |
<widget class="QPushButton" name="back" > | |
<property name="toolTip" > | |
<string>Back</string> | |
</property> | |
<property name="text" > | |
<string>Back</string> | |
</property> | |
<property name="icon" > | |
<iconset> | |
<normaloff>back.png</normaloff>back.png</iconset> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<widget class="QPushButton" name="next" > | |
<property name="enabled" > | |
<bool>true</bool> | |
</property> | |
<property name="toolTip" > | |
<string>Next</string> | |
</property> | |
<property name="layoutDirection" > | |
<enum>Qt::RightToLeft</enum> | |
</property> | |
<property name="text" > | |
<string>Next</string> | |
</property> | |
<property name="icon" > | |
<iconset> | |
<normaloff>next.png</normaloff>next.png</iconset> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<widget class="QPushButton" name="stop" > | |
<property name="toolTip" > | |
<string>Stop</string> | |
</property> | |
<property name="text" > | |
<string>Stop</string> | |
</property> | |
<property name="icon" > | |
<iconset> | |
<normaloff>stop.png</normaloff>stop.png</iconset> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<widget class="QPushButton" name="reload" > | |
<property name="toolTip" > | |
<string>Reload</string> | |
</property> | |
<property name="text" > | |
<string>Reload</string> | |
</property> | |
<property name="icon" > | |
<iconset> | |
<normaloff>reload.png</normaloff>reload.png</iconset> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<widget class="QLineEdit" name="url" /> | |
</item> | |
</layout> | |
</item> | |
<item> | |
<widget class="QWebView" name="webView" > | |
<property name="url" > | |
<url> | |
<string>about:blank</string> | |
</url> | |
</property> | |
</widget> | |
</item> | |
</layout> | |
</widget> | |
<customwidgets> | |
<customwidget> | |
<class>QWebView</class> | |
<extends>QWidget</extends> | |
<header>QtWebKit/QWebView</header> | |
</customwidget> | |
</customwidgets> | |
<resources/> | |
<connections/> | |
</ui> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from PyQt4.QtWebKit import * | |
app = QApplication(sys.argv) | |
web = QWebView() | |
web.load(QUrl("http://evolux.digi.com.br:8080/monitor/queue")) | |
web.show() | |
sys.exit(app.exec_()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
from PyQt4 import QtCore, QtGui | |
from httpWidget import Ui_HttpWidget | |
class httpWidget(QtGui.QWidget): | |
def __init__(self, parent=None): | |
super(httpWidget, self).__init__(parent) | |
self.ui = Ui_HttpWidget() | |
self.ui.setupUi(self) | |
# set margins | |
l = self.layout() | |
l.setMargin(0) | |
self.ui.horizontalLayout.setMargin(5) | |
# set the default | |
url = 'http://localhost:8080' | |
self.ui.url.setText(url) | |
# load page | |
self.ui.webView.setUrl(QtCore.QUrl(url)) | |
# history buttons: | |
self.ui.back.setEnabled(False) | |
self.ui.next.setEnabled(False) | |
QtCore.QObject.connect(self.ui.back,QtCore.SIGNAL("clicked()"), self.back) | |
QtCore.QObject.connect(self.ui.next,QtCore.SIGNAL("clicked()"), self.next) | |
QtCore.QObject.connect(self.ui.url,QtCore.SIGNAL("returnPressed()"), self.url_changed) | |
QtCore.QObject.connect(self.ui.webView,QtCore.SIGNAL("linkClicked (const QUrl&)"), self.link_clicked) | |
QtCore.QObject.connect(self.ui.webView,QtCore.SIGNAL("urlChanged (const QUrl&)"), self.link_clicked) | |
QtCore.QObject.connect(self.ui.webView,QtCore.SIGNAL("loadProgress (int)"), self.load_progress) | |
QtCore.QObject.connect(self.ui.webView,QtCore.SIGNAL("titleChanged (const QString&)"), self.title_changed) | |
QtCore.QObject.connect(self.ui.reload,QtCore.SIGNAL("clicked()"), self.reload_page) | |
QtCore.QObject.connect(self.ui.stop,QtCore.SIGNAL("clicked()"), self.stop_page) | |
QtCore.QMetaObject.connectSlotsByName(self) | |
def url_changed(self): | |
""" | |
Url have been changed by user | |
""" | |
page = self.ui.webView.page() | |
history = page.history() | |
if history.canGoBack(): | |
self.ui.back.setEnabled(True) | |
else: | |
self.ui.back.setEnabled(False) | |
if history.canGoForward(): | |
self.ui.next.setEnabled(True) | |
else: | |
self.ui.next.setEnabled(False) | |
url = self.ui.url.text() | |
self.ui.webView.setUrl(QtCore.QUrl(url)) | |
def stop_page(self): | |
""" | |
Stop loading the page | |
""" | |
self.ui.webView.stop() | |
def title_changed(self, title): | |
""" | |
Web page title changed - change the tab name | |
""" | |
self.setWindowTitle(title) | |
def reload_page(self): | |
""" | |
Reload the web page | |
""" | |
self.ui.webView.setUrl(QtCore.QUrl(self.ui.url.text())) | |
def link_clicked(self, url): | |
""" | |
Update the URL if a link on a web page is clicked | |
""" | |
page = self.ui.webView.page() | |
history = page.history() | |
if history.canGoBack(): | |
self.ui.back.setEnabled(True) | |
else: | |
self.ui.back.setEnabled(False) | |
if history.canGoForward(): | |
self.ui.next.setEnabled(True) | |
else: | |
self.ui.next.setEnabled(False) | |
self.ui.url.setText(url.toString()) | |
def load_progress(self, load): | |
""" | |
Page load progress | |
""" | |
if load == 100: | |
self.ui.stop.setEnabled(False) | |
else: | |
self.ui.stop.setEnabled(True) | |
def back(self): | |
""" | |
Back button clicked, go one page back | |
""" | |
page = self.ui.webView.page() | |
history = page.history() | |
history.back() | |
if history.canGoBack(): | |
self.ui.back.setEnabled(True) | |
else: | |
self.ui.back.setEnabled(False) | |
def next(self): | |
""" | |
Next button clicked, go to next page | |
""" | |
page = self.ui.webView.page() | |
history = page.history() | |
history.forward() | |
if history.canGoForward(): | |
self.ui.next.setEnabled(True) | |
else: | |
self.ui.next.setEnabled(False) | |
if __name__ == "__main__": | |
app = QtGui.QApplication(sys.argv) | |
myapp = httpWidget() | |
myapp.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From http://www.rkblog.rk.edu.pl/w/p/webkit-pyqt-rendering-web-pages/