我正在尝试处理createWindow
PyQt webkit 中的方法。当你这样做时,PyQt 会给出方法 a QWebPage.WebWindowType
(或QWebPage::WebWindowType
在 c++ 中)。我只需要一个 URL 来创建一个新窗口,所以我不确定如何处理这个类。文档也没有很好地解释它:http: //doc.qt.digia.com/qt/qwebpage.html#WebWindowType-enum
我正在尝试将其与我的“Tab”类集成:
class Tab(QtGui.QWidget):
"""
Contains the variables and methods needed to open the Qt
based user interface and the event bus to allow interaction.
"""
def __init__(self, url, window):
"""
Set the up the following:
1) Initialise the Qt user interface.
2) Set margins.
3) Initialise the cookie jar.
4) Set the default URL.
5) Set the history buttons to disabled (there is not history yet).
5) Hide the tools menu.
6) Create the GUI event bus.
7) Set keyboard bindings.
"""
self.window = window
QtGui.QWidget.__init__(self)
self.ui = Ui_Browser()
self.ui.setupUi(self)
l = self.layout()
l.setMargin(0)
self.ui.horizontalLayout.setMargin(0)
try:
self.ui.webView.page().networkAccessManager().setCookieJar(self.window.cookies)
except:
pass
self.ui.webView.setUrl(url)
self.ui.url.setText(url.toString())
self.ui.back.setEnabled(False)
self.ui.forward.setEnabled(False)
self.ui.toolsBox.hide()
self.ui.tools.setChecked(False)
QtCore.QObject.connect(self.ui.back, QtCore.SIGNAL("clicked()"), self.back)
QtCore.QObject.connect(self.ui.forward, QtCore.SIGNAL("clicked()"), self.forward)
QtCore.QObject.connect(self.ui.refresh, QtCore.SIGNAL("clicked()"), self.refresh)
QtCore.QObject.connect(self.ui.url, QtCore.SIGNAL("returnPressed()"), self.url)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("linkClicked (const QUrl&)"), self.navigate)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("urlChanged (const QUrl&)"), self.navigate)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("titleChanged(const QString&)"), self.title)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadStarted()"), self.showProgressBar)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadProgress(int)"), self.setProgressBar)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadFinished(bool)"), self.hideProgressBar)
QtCore.QObject.connect(self.ui.toolPrint, QtCore.SIGNAL("clicked()"), self.printPage)
self.ui.webView.createWindow = self.createWindow
QtGui.QShortcut(QtGui.QKeySequence("Backspace"), self, self.back)
QtGui.QShortcut(QtGui.QKeySequence("F5"), self, self.refresh)
QtGui.QShortcut(QtGui.QKeySequence("Ctrl+p"), self, self.printPage)
def url(self):
"""
Go to URL entered.
"""
self.setHistory()
url = self.ui.url.text()
if not str(url).startswith("http://"):
url = "http://" + url
self.ui.webView.setUrl(QtCore.QUrl(url))
def navigate(self, url):
"""
Navigate to page requested.
"""
self.setHistory()
self.ui.url.setText(url.toString())
def refresh(self):
"""
Refresh the page.
"""
self.ui.webView.setUrl(QtCore.QUrl(self.ui.webView.url()))
def back(self):
"""
Enable the back button.
"""
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 forward(self):
"""
Enable the forward button.
"""
page = self.ui.webView.page()
history = page.history()
history.forward()
if history.canGoForward():
self.ui.forward.setEnabled(True)
else:
self.ui.forward.setEnabled(False)
def title(self, title):
"""
Change the title of the window.
"""
self.window.tabs.setTabText(self.window.tabs.indexOf(self), title) or (self.window.setWindowTitle(title) if self.isThisFocused() else "New Tab")
def createWindow(self, WebWindowType):
"""
Overide the default behaviour.
"""
tab = Tab(QtCore.QUrl("about:blank"), self.window)
print(mode)
if mode == QtWebKit.QWebPage.WebModalDialog:
tab.ui.webView.setWindowModality(QtCore.Qt.ApplicationModal)
self.window.tabs.setCurrentIndex(self.window.tabs.addTab(tab, ""))
def isThisFocused(self):
"""
Return whether this tab is focused or not.
"""
if self.window.tabs.currentWidget() == self:
return True
return False
def setHistory(self):
"""
Check history and update buttons.
"""
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.forward.setEnabled(True)
else:
self.ui.forward.setEnabled(False)
def showProgressBar(self):
"""
We're loading a page, show the load progress bar.
"""
self.ui.progress.show()
def setProgressBar(self, percent):
"""
Set the percentage of the progress bar.
"""
self.ui.progress.setValue(percent)
def hideProgressBar(self, bool):
"""
We've finished loading the page, there is no need for the progress bar.
"""
self.ui.progress.hide()
def printPage(self):
"""
Use Qt's goodness to print the page.
"""
previewer = QtGui.QPrintPreviewDialog(paintRequested=self.ui.webView.print_)
previewer.exec_()