Advertisement
Guest User

SIMPLE PYQT BROWSER

a guest
Jan 7th, 2011
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from PyQt4.QtCore import *
  3. from PyQt4.QtNetwork import *
  4. # -*- coding: utf-8 -*-
  5. from PyQt4.QtCore import *
  6. from PyQt4.QtNetwork import *
  7. from PyQt4.QtGui import *
  8. from PyQt4.QtWebKit import *
  9.  
  10. class BaseBrowser(QWidget):
  11.     def __init__(self,  parent = None):
  12.         super(BaseBrowser, self).__init__(parent)
  13.         self.__progress = 0
  14.         QNetworkProxyFactory.setUseSystemConfiguration(True)
  15.         self.webView = QWebView()
  16.         self.webView.load(QUrl("http://www.yandex.ru"))
  17.         self.connect(self.webView, SIGNAL("loadFinished(bool)"), self.adjustLocation)
  18.         self.connect(self.webView, SIGNAL("titleChanged(QString)"), self.adjustTitle)
  19.         self.connect(self.webView, SIGNAL("loadProgress(int)"), self.setProgress)
  20.         self.connect(self.webView, SIGNAL("loadFinished(bool)"), self.finishLoading)
  21.         self.locationEdit = QLineEdit()
  22.         self.locationEdit.setSizePolicy(QSizePolicy.Expanding, self.locationEdit.sizePolicy().verticalPolicy())
  23.         self.connect(self.locationEdit, SIGNAL("returnPressed()"), self.changeLocation)
  24.         self.goButton = QPushButton("Go")
  25.         self.connect(self.goButton, SIGNAL("clicked()"),  self.changeLocation)
  26.         self.layout = QGridLayout(self)
  27.         self.layout.addWidget(self.locationEdit, 0, 0)
  28.         self.layout.addWidget(self.goButton, 0, 1)
  29.         self.layout.addWidget(self.webView, 1, 0,  1,  2)
  30.         self.setLayout(self.layout)
  31.        
  32.     def adjustLocation(self):
  33.         self.locationEdit.setText(self.webView.url().toString())
  34.        
  35.     def changeLocation(self):
  36.         url = self.locationEdit.text()
  37.         if url[0:7] != 'http://':
  38.             url = 'http://' + url
  39.         self.webView.load(QUrl(url))
  40.         self.webView.setFocus()
  41.        
  42.     def adjustTitle(self):
  43.         if self.__progress <= 0 or self.__progress >= 100:
  44.             self.setWindowTitle(self.webView.title())
  45.         else:
  46.             self.setWindowTitle(QString("%1 (%2%)").arg(self.webView.title()).arg(self.__progress))
  47.            
  48.     def setProgress(self,  p):
  49.         self.__progress = p
  50.         self.adjustTitle()
  51.        
  52.     def finishLoading(self):
  53.         self.__progress = 100
  54.         self.adjustTitle()
  55.  
  56. if __name__ == "__main__":
  57.     import sys
  58.     app = QApplication(sys.argv)
  59.     prog = BaseBrowser()
  60.     prog.show()
  61.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement