Skip to content Skip to sidebar Skip to footer

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 prac

Solution 1:

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_())

Post a Comment for "How To Make Tabbed Browser With Qwebengine"