使用pdf路径更新QWebEngineView

2024-09-30 22:14:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QtWebEngineWidgets显示一些pdf文件。我想更改pdf并强制QtWebEngineView自动动态显示。我得到的问题是QtWebEngineWidgets没有更新,当pdf文件路径改变时无法显示。在

class PdfReport(QtWebEngineWidgets.QWebEngineView):
    PDFJS = 'file:///pdfjs/web/viewer.html'
    def __init__(self, parent=None):
        super(PdfReport, self).__init__(parent)
        self.PDF = 'file:///Technicalreport/file0.pdf'
        self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, self.PDF))) 

    @QtCore.pyqtSlot(int)    
    def index_load(self, _index):
        self._index = _index
        self.PDF = pdfpath(self._index)

外部功能:

^{pr2}$

尝试测试函数并按预期返回:

for i in range(3):
    print(pdfpath(i), type(pdfpath(i)))

file:///Technicalreport/file0.pdf <class 'str'>
file:///Technicalreport/file1.pdf <class 'str'>
file:///Technicalreport/file2.pdf <class 'str'>

{cd6>

运行此错误时获取:

TypeError: 'module' object is not callable

更新:

import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

PDFJS = 'file:///pdfjs/web/viewer.html'
PDF = 'file:///Technicalreport/file0.pdf'

def pdfpath(index):
    if index == -1:
        PDF = 'file:///Technicalreport/file0.pdf'
    else:
        PDF = 'file:///Technicalreport/file%d.pdf' %index
    return PDF


class Foo(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)
        self.setGeometry(QtCore.QRect(200, 100, 800, 800))

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

        self.com.IndexChanged.connect(self.pdf.index_load)


class Window(QtWebEngineWidgets.QWebEngineView):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.PDF = PDF
        self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, self.PDF)))            

    @QtCore.pyqtSlot(int)    
    def index_load(self, _index):
        self._index = _index
        self.PDF = pdfpath(self._index)
        print(self.PDF,'=', self._index)



class Widget(QtWidgets.QWidget):
    IndexChanged = QtCore.pyqtSignal(int)
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.combo = QtWidgets.QComboBox(self)
        self.layout().addWidget(self.combo)
        self.combo.addItems(["item1", "item2", "item3"])
        self.combo.setMinimumWidth(150)
        self.combo.activated[int].connect(self.onActivatedIndex)

    @QtCore.pyqtSlot(int)
    def onActivatedIndex(self, index):
        self.IndexChanged.emit(index)


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Foo()
    window.setGeometry(600, 50, 800, 600)
    window.show()
    sys.exit(app.exec_())

显示:

enter image description here


Tags: selfindexpdfinitdefloadclassfile
1条回答
网友
1楼 · 发布于 2024-09-30 22:14:03

假设程序的其他部分正常工作,问题是您只更新了一个变量,但没有加载新的url,因此解决方案是:

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

在您的例子中,问题是您创建的路径不正确,因为您必须使用绝对路径,而不是像您的例子中那样使用相对路径。考虑到上述情况,解决方案是:

^{pr2}$
├── main.py
├── pdfjs
│   ├── build
│   │   └── ...
│   ├── LICENSE
│   └── web
│       ├── ...
│       ├── viewer.html
│       └── ...
└── Technicalreport
    ├── file0.pdf
    ├── file1.pdf
    └── file2.pdf

相关问题 更多 >