0

After I wrote this code the window is not displayed but the console messages are being displayed in the terminal

I tried add few extra pyqt5 import statements but nothng works

class MyPage(QWebEnginePage):
    def __init__(self, parent=None):
        super(MyPage, self).__init__(parent)
    def triggerAction(self, action, checked=False):
        if action == QWebEnginePage.OpenLinkInNewWindow:
            self.createWindow(QWebEnginePage.WebBrowserWindow)

        return super(MyPage, self).triggerAction(action, checked)


class MyWindow(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.myPage = MyPage(self)

        self.setPage(self.myPage)

    def createWindow(self, windowType):
        if windowType == QWebEnginePage.WebBrowserTab:
            self.webView = MyWindow()
            self.webView.setAttribute(Qt.WA_DeleteOnClose, True)
            self.webView.show()
            return self.webView
        return super(MyWindow, self).createWindow(windowType)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()
    main.load(QUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target"))

    sys.exit(app.exec_())

Need to know the exact import statements and display the window where the HTML is viewed.

1 Answers1

1

There is a bug with the size of the QWebEngineView that has the solution to manually set a size:

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


class MyPage(QtWebEngineWidgets.QWebEnginePage):
    def triggerAction(self, action, checked=False):
        if action == QtWebEngineWidgets.QWebEnginePage.OpenLinkInNewWindow:
            self.createWindow(
                QtWebEngineWidgets.QWebEnginePage.WebBrowserWindow
            )
        return super(MyPage, self).triggerAction(action, checked)


class MyWindow(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.myPage = MyPage(self)
        self.setPage(self.myPage)

    def createWindow(self, windowType):
        if windowType == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            webView = MyWindow(self)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(640, 480) # <----
            webView.show()
            return webView
        return super(MyWindow, self).createWindow(windowType)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName("MyWindow")

    main = MyWindow()
    main.resize(640, 480) # <----
    main.show()
    main.load(
        QtCore.QUrl(
            "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target"
        )
    )

    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241