I have this code that is supposed to visit/follow any link that I click in the same window, even if it would normally open in a new window. This would be instead of having to right-click and then select "Follow link" from context menu. For some reason, it doesn't work as expected.
Here is the code:
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class WebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
return True
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
class HtmlView(QWebEngineView):
def __init__(self, *args, **kwargs):
QWebEngineView.__init__(self, *args, **kwargs)
self.setPage(WebEnginePage(self))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = HtmlView()
w.load(QUrl("https://yahoo.com"));
w.show()
sys.exit(app.exec_())
If you want links to always open in the same window, you can reimplement the createWindow method, so that it returns the same view:
class HtmlView(QWebEngineView):
def createWindow(self, wintype):
return self
The wintype argument provides information about which type of window is being requested. You may want to treat dialog windows differently.
Note that the WebEnginePage subclass in your example is not needed for this to work.
Related
I need to open an URL using pyQt5. The page has several links that open a new window. pyQt5 opens a windows for the URL but does not do anything after clicking on a link that should open a new window.
P.S I'm using pyQt5.6
I have tried it on Linux centOs but nothing works.
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class WebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
return True
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
class HtmlView(QWebEngineView):
def __init__(self, *args, **kwargs):
QWebEngineView.__init__(self, *args, **kwargs)
self.setPage(WebEnginePage(self))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = HtmlView()
w.load(QUrl("https://gmail.com"));
w.show()
sys.exit(app.exec_())
I expect it to open a new window on click of target='_blank' on any webpage.
You have to override the createWindow method and return a QWebEngineView, but for the object not to be distruded it must be the child of another window or be part of a container that has a longer life cycle.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
return True
return super(WebEnginePage, self).acceptNavigationRequest(url, _type, isMainFrame)
class HtmlView(QtWebEngineWidgets.QWebEngineView):
def __init__(self, windows, *args, **kwargs):
super(HtmlView, self).__init__(*args, **kwargs)
self.setPage(WebEnginePage(self))
self._windows = windows
self._windows.append(self)
def createWindow(self, _type):
if QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
v = HtmlView(self._windows)
v.resize(640, 480)
v.show()
return v
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
windows = []
w = HtmlView(windows)
w.load(QtCore.QUrl("https://gmail.com"));
w.show()
sys.exit(app.exec_())
So my expected outcome is for the login.ui to show when the login button is clicked. My code reached the def gotologin function and the class LoginScreen , but it doesn't load the ui
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('welcomescreen.ui', self)
self.login.clicked.connect(self.gotologin)
def gotologin(self):
login = LoginScreen()
widget.addWidget(login)
widget.setCurrentIndex(widget.currentIndex()+1)
class LoginScreen(QDialog):
def __init__(self):
super(LoginScreen, self).__init__()
loadUi('login.ui', self)
app = QApplication(sys.argv)
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedHeight(800)
widget.setFixedWidth(1200)
window = WelcomeScreen()
window.show()
try:
sys.exit(app.exec_())
except:
print('Exiting')
Your logic is strange since you are creating 2 WelcomeScreen: One added to the QStackedWidget and the second as a window. Besides that, the QStackedWidget has never been shown. And as the icing on the cake you don't limit the scopes of the variables.
Considering the above, it is better to create a controller that implements the logic of switching widgets and limits scopes.
import sys
from functools import cached_property
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication, QDialog, QStackedWidget
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi("welcomescreen.ui", self)
class LoginScreen(QDialog):
def __init__(self):
super(LoginScreen, self).__init__()
loadUi("login.ui", self)
class Controller:
def __init__(self):
self.stacked_widget.addWidget(self.welcome)
self.stacked_widget.addWidget(self.login)
self.welcome.login.clicked.connect(self.goto_login)
#cached_property
def stacked_widget(self):
return QStackedWidget()
#cached_property
def welcome(self):
return WelcomeScreen()
#cached_property
def login(self):
return LoginScreen()
def goto_login(self):
self.stacked_widget.setCurrentWidget(self.login)
def main(args):
app = QApplication(args)
controller = Controller()
controller.stacked_widget.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
And finally, do not silence the exceptions since their reason for being is to indicate that something is wrong. I prefer that when the program fails then it shouts at me that a silent error since the latter is the cause of many bugs.
I need to open an URL using pyQt5. The page has several links that open a new window. pyQt5 opens a windows for the URL but does not do anything after clicking on a link that should open a new window.
P.S I'm using pyQt5.6
I have tried it on Linux centOs but nothing works.
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class WebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
return True
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
class HtmlView(QWebEngineView):
def __init__(self, *args, **kwargs):
QWebEngineView.__init__(self, *args, **kwargs)
self.setPage(WebEnginePage(self))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = HtmlView()
w.load(QUrl("https://gmail.com"));
w.show()
sys.exit(app.exec_())
I expect it to open a new window on click of target='_blank' on any webpage.
You have to override the createWindow method and return a QWebEngineView, but for the object not to be distruded it must be the child of another window or be part of a container that has a longer life cycle.
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
return True
return super(WebEnginePage, self).acceptNavigationRequest(url, _type, isMainFrame)
class HtmlView(QtWebEngineWidgets.QWebEngineView):
def __init__(self, windows, *args, **kwargs):
super(HtmlView, self).__init__(*args, **kwargs)
self.setPage(WebEnginePage(self))
self._windows = windows
self._windows.append(self)
def createWindow(self, _type):
if QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
v = HtmlView(self._windows)
v.resize(640, 480)
v.show()
return v
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
windows = []
w = HtmlView(windows)
w.load(QtCore.QUrl("https://gmail.com"));
w.show()
sys.exit(app.exec_())
I have the following code snippet working in PySide and need to translate it to work in PySide2.
The purpose is to force all links to open in the system browser when clicked (rather than the widget trying to load them):
from PySide.QtWebKit import QWebView, QWebPage
class HtmlView(QWebView):
def __init__(self, parent=None):
super(HtmlView, self).__init__(parent)
self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) # not working in PySide2
self.linkClicked.connect(self.openWebsite) # not working in PySide2
This was my attempt of a translation:
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class HtmlView(QWebEngineView):
def __init__(self, parent=None):
super(HtmlView, self).__init__(parent)
self.page().setLinkDelegationPolicy(QWebEnginePage.DelegateAllLinks) # not working in PySide2
self.linkClicked.connect(self.openWebsite) # not working in PySide2
However, QWebEngineView.linkClicked does not exist and neither does QWebEngineView.setLinkDelegationPolicy or
QWebEnginePage.DelegateAllLinks.
What is the best way to achieve this in PySide2 without the above?
Edit: I checked the QEvents that are triggered but no event seems to be fired off when a link is clicked, so without the linkClicked event from PySide/Qt4.8 I have no idea how to hook into this.
Thanks,
frank
You have to use acceptNavigationRequest:
This function is called upon receiving a request to navigate to the
specified url by means of the specified navigation type type.
isMainFrame indicates whether the request corresponds to the main
frame or a child frame. If the function returns true, the navigation
request is accepted and url is loaded. The default implementation
accepts all navigation requests.
In your case you must reject and open the url when the type is QWebEnginePage::NavigationTypeLinkClicked.
from PySide2.QtCore import QUrl
from PySide2.QtGui import QDesktopServices
from PySide2.QtWidgets import QApplication
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class WebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
QDesktopServices.openUrl(url);
return False
return True
class HtmlView(QWebEngineView):
def __init__(self, *args, **kwargs):
QWebEngineView.__init__(self, *args, **kwargs)
self.setPage(WebEnginePage(self))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = HtmlView()
w.load(QUrl("https://stackoverflow.com/questions/47736408/pyside2-qwebview-how-to-open-links-in-system-browser"));
w.show()
sys.exit(app.exec_())
I'm trying to get a QTreeView to allow the user to edit file names from a QFileSystemModel. However Qt just prints:
edit: editing failed
I get the same result with PySide and PyQt.
Opening the editor with openPersistenEditor() works, but I would prefer to use the build-in mechanism.
import sys
from PyQt4.QtGui import QTreeView, QFileSystemModel, QApplication
class TestView(QTreeView):
def __init__(self, directory, *args, **kwargs):
super(TestView, self).__init__(*args, **kwargs)
self.file_system_model = QFileSystemModel()
self.file_system_model.setRootPath(directory)
index = self.file_system_model.index(directory)
self.setModel(self.file_system_model)
self.setRootIndex(index)
self.activated.connect(self._on_edit)
def _on_edit(self, index):
# self.closePersistentEditor(index)
# app.processEvents()
self.edit(self.currentIndex())
# self.openPersistentEditor(index)
if __name__ == '__main__':
app = QApplication([])
directory = r'c:/'
dialog = TestView(directory)
dialog.show()
sys.exit(app.exec_())
The model is read-only by default, so you need to add:
self.file_system_model.setReadOnly(False)