Multiple window open up when click on a link having pdf file in it

时间:2019-05-31 11:34:29

标签: python pyqt pyqt5 qtwebengine

when clicking on the URL having pdf file in it my browser opened up multiple windows.Although it should be open single new window. Below is the code which can explain the problem more clearly.

import sys

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl

from PyQt5.QtWidgets import QApplication


class MyWindow(QtWebEngineWidgets.QWebEngineView):

    currentFile = ''

    def __init__(self,windows, parent=None):
        super(MyWindow, self).__init__()

        self._windows = windows
        self._windows.append(self)
        self.page().profile().downloadRequested.connect(self._downloadRequested)


        self.load(QUrl.fromUserInput("file:///C:/Users/TCI/Desktop/Aditya/test/default.html"))
        # self.load(QUrl("https://www.gmail.com"))
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)  
        self.show()

    def createWindow(self, windows):
        print(windows)
        if windows == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            webView = MyWindow(self._windows)
            webView.resize(900, 780) # <----
            return webView

        elif windows == QtWebEngineWidgets.QWebEnginePage.WebDialog:
            webView = MyWindow(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)           
            webView.resize(900, 780) # <----

            webView.show()          
            return webView

        elif windows == QtWebEngineWidgets.QWebEnginePage.acceptNavigationRequest:
            webView = MyWindow(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)           
            webView.resize(900, 780) # <----
            webView.show()          
            return webView
        return super(MyWindow, self).createWindow(windows)

    def on_download_finish(self):
        #If downloaded file is PDF then handle it accordingly
        if str(self.currentFile).split('.')[-1] == 'pdf':
            self.w = SecondWindow(self.currentFile)
            self.w.show()

    def _downloadRequested(self, item): # QWebEngineDownloadItem
        self.currentFile = str(item.path()).replace('\\', '/')
        item.accept()
        item.finished.connect(self.on_download_finish)


class SecondWindow(QtWebEngineWidgets.QWebEngineView):
    def __init__(self,currentFile):
        super(SecondWindow, self).__init__()
        print('Handle PDF file')
        #Code to handle PDF File




if __name__ == "__main__":
    app = QApplication(sys.argv)
    windows = []
    main = MyWindow(windows)  
    sys.exit(app.exec_())

Below is the html page having a url link in it which again redirecting it to a pdf file

<html>
    <head>
        <title>URL Error</title>
    </head>
    <body>
        <center style="padding-top:100px">
            <h2>Invaid URL</h2>
            <p>No URL passed in argument!</p>

        </center>
        <a href="#" onclick="javascript:window.open('https://www3.nd.edu/~rwilliam/stats1/x13.pdf','zajaz','resizable=yes,status=no,location=no,menubar=no,scrollbars=yes')" class="btn btn-freight-dash btn-top"><span class="glyphicon glyphicon-book btn-top"></span>Manual</a>
    </body>
</html>

Above code is running well. But multiple windows are opening.

0 个答案:

没有答案
相关问题