I have a QtWebEngineWidgets
showing some pdf files. I want to change pdf and force QtWebEngineView
display automatically and dynamically. The problem I get is the QtWebEngineWidgets
does not update, incapabile to display when pdf file path changes.
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)
Outer function:
def pdfpath(index):
if index == -1:
PDF = 'file:///Technicalreport/file0.pdf'
else:
PDF = 'file:///Technicalreport/file%d.pdf' %index
return PDF
tried to test function and returns as expected:
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'>
Yes pdf files 'file0'
, 'file1'
and 'file2'
exist:
When it runs gets this error:
TypeError: 'module' object is not callable
Update:
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_())
Display:
See Question&Answers more detail:
os