PySide QtWebKit 读取网页

  
    
1 # coding:utf8
2
3 import sys
4 from PySide import QtCore
5 from PySide import QtGui
6 from PySide import QtWebKit
7 from PySide import QtNetwork
8 from pprint import pprint
9
10 class Mainwin(QtGui.QMainWindow):
11 def __init__ (self, parent = None):
12 super(Mainwin, self). __init__ ( parent)
13
14 QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)
15
16 splitter = self.splitter = QtGui.QSplitter(self)
17
18 self.view = QtWebKit.QWebView(splitter)
19 self.view.load( " about:blank " )
20 self.view.loadStarted.connect(self.adjustLocation)
21 self.view.titleChanged.connect(self.adjustTitle)
22 self.view.loadProgress.connect(self.setProgress)
23 self.view.loadFinished.connect(self.adjustLocation)
24
25 self.locationEdit = QtGui.QLineEdit(self)
26 self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,
27 self.locationEdit.sizePolicy().verticalPolicy())
28 self.locationEdit.returnPressed.connect(self.changeLocation)
29
30 toolBar = self.addToolBar( " Navigation " )
31 toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Back))
32 toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Forward))
33 toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Reload))
34 toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Stop))
35 act = QtGui.QAction(self)
36 toolBar.addAction(act)
37 act.triggered.connect(self.loadPage)
38 toolBar.addWidget(self.locationEdit)
39
40 viewMenu = self.menuBar().addMenu( " &View " )
41 viewSourceAction = QtGui.QAction( " Page Source " , self)
42 viewSourceAction.triggered.connect(self.viewSource)
43 viewMenu.addAction(viewSourceAction)
44
45 # listview = QtGui.QListView()
46 # splitter.addWidget(listview)
47 # treeview = QtGui.QTreeView()
48 # splitter.addWidget(treeview)
49 textedit = QtGui.QTextEdit()
50 splitter.addWidget(textedit)
51
52 splitter.addWidget(self.view)
53
54 self.setCentralWidget(splitter)
55
56 sb = self.createStatusbar()
57 self.setStatusBar(sb)
58 page = self.view.page()
59 page.linkHovered.connect(self.linkHovered)
60 # act.connect(sb.showMessage)
61
62 def loadPage(self):
63 self.view.load( " http://localhost " )
64
65 def linkHovered(self, p):
66 sb = self.statusBar()
67 sb.showMessage(p)
68
69 def viewSource(self):
70 accessManager = self.view.page().networkAccessManager()
71 request = QtNetwork.QNetworkRequest(self.view.url())
72 reply = accessManager.get(request)
73 reply.finished.connect(self.slotSourceDownloaded)
74
75 def slotSourceDownloaded(self):
76 reply = self.sender()
77 self.textEdit = QtGui.QTextEdit(None)
78 self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)
79 self.textEdit.show()
80 self.textEdit.setPlainText(QtCore.QTextStream(reply).readAll())
81 self.textEdit.resize( 600 , 400 )
82 reply.deleteLater()
83
84 def createStatusbar(self):
85 sb = self.statusBar()
86 sb.progress = QtGui.QProgressBar()
87 sb.addPermanentWidget(sb.progress)
88 return sb
89
90 def adjustTitle(self):
91 self.setWindowTitle(self.view.title())
92
93 def adjustLocation(self):
94 self.locationEdit.setText(self.view.url().toString())
95
96 def changeLocation(self):
97 url = QtCore.QUrl.fromUserInput(self.locationEdit.text())
98 self.view.load(url)
99 self.view.setFocus()
100
101 def setProgress(self, progress):
102 self.statusBar().progress.setValue(progress)
103
104
105 def main():
106 app = QtGui.QApplication(sys.argv)
107 mw = Mainwin()
108 mw.show()
109 app.exec_()
110
111
112 if __name__ == " __main__ " :
113 main()
代码很简单,PySide功能真强大!

你可能感兴趣的:(webkit)