pythonpython-3.xpyqt5qwebview

how to make tabbed browser with qwebengine


I have some pyqt5 code that I am trying to make tabbed browser and how when I click target _blank link it opens in new tab with that link. Also, As I am still new to PyQt5 and practicing thorugh this project so I need some help.In the code I have 3 classes one holds mainwindow and one holds webview and one holds webpage. Here is the code:

class Ui_MainWindow(QMainWindow):

    def __init__(self):
         super(Ui_MainWindow, self).__init__()
         self.setupUi(self)

    def setupUi(self, MainWindow):
         self.centralwidget = QtWidgets.QWidget(MainWindow)
         self.centralwidget.setObjectName("centralwidget")
         self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
         self.tabWidget.setGeometry(QtCore.QRect(0, 75, width, height))
         self.tabWidget.setObjectName("tabWidget")
         self.loadUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target")
    def loadUrl(self, url):
        self.b = HtmlView()
        self.tabWidget.setCurrentIndex(self.tabWidget.addTab(self.b, 'loading...'))
        self.b.load(QUrl(url))
class WebEnginePage(QWebEnginePage):
    def __init__(self, parent=None):
         super(WebEnginePage, self).__init__(parent)
    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        if (_type == QWebEnginePage.NavigationTypeLinkClicked):
             Ui_MainWindow.loadUrl(Ui_MainWindow, url)
             return True
        return QWebEnginePage.acceptNavigationRequest(self, url,  _type, isMainFrame)
class HtmlView(QWebEngineView):
    def __init__(self, parent=None):
        super(HtmlView, self).__init__()
        self.setPage(WebEnginePage(self))

Solution

  • The classes are abstractions, that is, they are useless but I create an object of the class and you in the instruction Ui_MainWindow.loadUrl(Ui_MainWindow, url) are using a class without creating an object, what you must do is use the same object Through the instance as I show below:

    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtWebEngineWidgets import *
    
    class TabWidget(QTabWidget):
        def __init__(self, *args, **kwargs):
            QTabWidget.__init__(self, *args, **kwargs)
            url = QUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target")
            view = HtmlView(self)
            view.load(url)
            ix = self.addTab(view, "loading ...")
    
    class HtmlView(QWebEngineView):
        def __init__(self, *args, **kwargs):
            QWebEngineView.__init__(self, *args, **kwargs)
            self.tab = self.parent()
    
        def createWindow(self, windowType):
            if windowType == QWebEnginePage.WebBrowserTab:
                webView = HtmlView(self.tab)
                ix = self.tab.addTab(webView, "loading ...")
                self.tab.setCurrentIndex(ix)
                return webView
            return QWebEngineView.createWindow(self, windowType)
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv)
        main = TabWidget()
        main.show()
        sys.exit(app.exec_())