QWebEngineView update with pdf path(使用 pdf 路径更新 QWebEngineView)
问题描述
我有一个显示一些 pdf 文件的 QtWebEngineWidgets.我想更改 pdf 并强制 QtWebEngineView 自动动态显示.我得到的问题是 QtWebEngineWidgets 没有更新,无法在 pdf 文件路径更改时显示.
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)
外层函数:
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'>
存在pdf文件'file0'、'file1'和'file2':
运行时出现此错误:
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_())
显示:
推荐答案
假设你程序的其他部分工作正常,问题是你只是更新了一个变量而没有加载新的url,所以解决方法是:
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
这篇关于使用 pdf 路径更新 QWebEngineView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 pdf 路径更新 QWebEngineView
基础教程推荐
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
