QWebEngineView update with pdf path

核能气质少年 提交于 2019-12-02 03:21:44

Assuming that the other parts of your program work correctly, the problem is that you are only updating a variable but you are not loading the new url, so the solution is:

class PdfReport(QtWebEngineWidgets.QWebEngineView):
    PDFJS = "file:///pdfjs/web/viewer.html"

    def __init__(self, parent=None):
        super(PdfReport, self).__init__(parent)
        self.load_pdf("file:///Technicalreport/file0.pdf")

    def load_pdf(self, pdf):
        self.load(
            QtCore.QUrl.fromUserInput("%s?file=%s" % (PdfReport.PDFJS, pdf))
        )

The problem in your case is that you are creating the path incorrectly because you must use an absolute path and not relative as in your case. Considering the above the solution is:

import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

PDFJS = QtCore.QUrl.fromLocalFile(
    os.path.join(CURRENT_DIR, "pdfjs/web/viewer.html")
).toString()


def pdfpath(index):
    filename = ""
    if index == -1:
        filename = "Technicalreport/file0.pdf"
    else:
        filename = "Technicalreport/file%d.pdf" % index
    return os.path.join(CURRENT_DIR, filename)


class PdfReport(QtWebEngineWidgets.QWebEngineView):
    def load_pdf(self, filename):
        url = QtCore.QUrl.fromLocalFile(filename).toString()
        self.load(QtCore.QUrl.fromUserInput("%s?file=%s" % (PDFJS, url)))

    def sizeHint(self):
        return QtCore.QSize(640, 480)

    @QtCore.pyqtSlot(int)
    def index_load(self, index):
        path = pdfpath(index)
        self.load_pdf(path)


class Widget(QtWidgets.QWidget):
    indexChanged = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.combo = QtWidgets.QComboBox()
        self.combo.addItems(["item1", "item2", "item3"])
        self.combo.setMinimumWidth(150)
        self.combo.activated[int].connect(self.indexChanged)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.combo)

        self.setSizePolicy(
            QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum
        )


class Foo(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)

        self.pdf = PdfReport()
        self.com = Widget()

        self.com.indexChanged.connect(self.pdf.index_load)
        self.pdf.index_load(-1)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.pdf)
        lay.addWidget(self.com)


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    w = Foo()
    w.show()
    sys.exit(app.exec_())
├── main.py
├── pdfjs
│   ├── build
│   │   └── ...
│   ├── LICENSE
│   └── web
│       ├── ...
│       ├── viewer.html
│       └── ...
└── Technicalreport
    ├── file0.pdf
    ├── file1.pdf
    └── file2.pdf
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!