Python 带有pdf路径的QWebEngineView更新

Python 带有pdf路径的QWebEngineView更新,python,python-3.x,pyqt,pyqt5,qtwebengine,Python,Python 3.x,Pyqt,Pyqt5,Qtwebengine,我有一个QtWebEngineWidgets显示一些pdf文件。我想更改pdf并强制自动动态显示QtWebEngineView。我遇到的问题是QtWebEngineWidgets不更新,无法在pdf文件路径更改时显示 class PdfReport(QtWebEngineWidgets.QWebEngineView): PDFJS = 'file:///pdfjs/web/viewer.html' def __init__(self, parent=None):

我有一个
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)
外部功能:

def pdfpath(index):
    if index == -1:
        PDF = 'file:///Technicalreport/file0.pdf'
    else:
        PDF = 'file:///Technicalreport/file%d.pdf' %index
    return PDF
尝试测试函数并按预期返回:

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'>
更新:

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_())
显示:

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

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

类PdfReport(QtWebEngineWidgets.QWebEngineView):
PDFJS=”file:///pdfjs/web/viewer.html"
def uuu init uuu(self,parent=None):
超级(PdfReport,self)。\uuuuu初始化(父级)
self.load_pdf(“file:///Technicalreport/file0.pdf")
def load_pdf(self,pdf):
自负载(
QtCore.QUrl.fromUserInput(“%s”文件=%s“%(PdfReport.PDFJS,pdf))
)
本例中的问题是,您创建的路径不正确,因为您必须使用绝对路径,而不是像本例中那样使用相对路径。考虑到上述情况,解决方案是:

导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
CURRENT_DIR=os.path.dirname(os.path.realpath(uu文件_uu))
PDFJS=QtCore.QUrl.fromLocalFile(
join(当前目录“pdfjs/web/viewer.html”)
).toString()
def pdfpath(索引):
filename=“”
如果索引==-1:
filename=“Technicalreport/file0.pdf”
其他:
filename=“Technicalreport/文件%d.pdf”%index
返回os.path.join(当前目录,文件名)
类PdfReport(QtWebEngineWidgets.QWebEngineView):
def load_pdf(自身,文件名):
url=QtCore.QUrl.fromLocalFile(文件名).toString()
self.load(QtCore.QUrl.fromUserInput(“%s”文件=%s“%(PDFJS,url)))
def sizeHint(自身):
返回QtCore.QSize(640480)
@QtCore.pyqtSlot(int)
def索引_负载(自身,索引):
路径=pdfpath(索引)
self.load_pdf(路径)
类小部件(qtwidts.QWidget):
indexChanged=QtCore.pyqtSignal(int)
def uuu init uuu(self,parent=None):
超级(小部件,自我)。\uuuuu初始化\uuuuuuu(父级)
self.combo=qtwidts.QComboBox()
self.combo.addItems([“item1”、“item2”、“item3]”)
self.combo.setMinimumWidth(150)
self.combo.activated[int].connect(self.indexChanged)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.combo)
自我设定策略(
qtwidts.QSizePolicy.Preferred,qtwidts.QSizePolicy.Maximum
)
类Foo(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
超级(Foo,self)。\uuuuu初始化\uuuuuuu(父级)
self.pdf=PdfReport()
self.com=Widget()
self.com.indexChanged.connect(self.pdf.index\u load)
self.pdf.index\u加载(-1)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.pdf)
lay.addWidget(self.com)
如果名称=“\uuuuu main\uuuuuuuu”:
app=qtwidts.QApplication(sys.argv)
w=Foo()
w、 show()
sys.exit(app.exec_())

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

类PdfReport(QtWebEngineWidgets.QWebEngineView):
PDFJS=”file:///pdfjs/web/viewer.html"
def uuu init uuu(self,parent=None):
超级(PdfReport,self)。\uuuuu初始化(父级)
self.load_pdf(“file:///Technicalreport/file0.pdf")
def load_pdf(self,pdf):
自负载(
QtCore.QUrl.fromUserInput(“%s”文件=%s“%(PdfReport.PDFJS,pdf))
)
本例中的问题是,您创建的路径不正确,因为您必须使用绝对路径,而不是像本例中那样使用相对路径。考虑到上述情况,解决方案是:

导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
CURRENT_DIR=os.path.dirname(os.path.realpath(uu文件_uu))
PDFJS=QtCore.QUrl.fromLocalFile(
join(当前目录“pdfjs/web/viewer.html”)
).toString()
def pdfpath(索引):
filename=“”
如果索引==-1:
filename=“Technicalreport/file0.pdf”
其他:
filename=“Technicalreport/文件%d.pdf”%index
返回os.path.join(当前目录,文件名)
类PdfReport(QtWebEngineWidgets.QWebEngineView):
def load_pdf(自身,文件名):
url=QtCore.QUrl.fromLocalFile(文件名).toString()
self.load(QtCore.QUrl.fromUserInput(“%s”文件=%s“%(PDFJS,url)))
def sizeHint(自身):
返回QtCore.QSize(640480)
@QtCore.pyqtSlot(int)
def索引_负载(自身,索引):
路径=pdfpath(索引)
self.load_pdf(路径)
类小部件(qtwidts.QWidget):
indexChanged=QtCore.pyqtSignal(int)
def uuu init uuu(self,parent=None):
超级(小部件,自我)。\uuuuu初始化\uuuuuuu(父级)
self.combo=qtwidts.QComboBox()
self.combo.addItems([“item1”、“item2”、“item3]”)
self.combo.setMinimumWidth(150)
self.combo.activated[int].connect(self.indexChanged)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.combo)
自我设定策略(
qtwidts.QSizePolicy.Preferred,qtwidts.QSizePolicy.Maximum
)
类Foo(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
超级(Foo,self)。\uuuuu初始化\uuuuuuu(父级)
self.pdf=PdfReport()
self.com=Widget()
self.com.indexChanged.connect(self.pdf.index\u load)
self.pdf.index\u加载(-1)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.pdf)
lay.addWidget(self.com)
如果名称=“\uuuuu main\uuuuuuuu”:
app=qtwidts.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