QWebEngineView用pdf路径更新

时间:2019-07-03 08:49:15

标签: 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):
        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'>

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

显示

enter image description here

1 个答案:

答案 0 :(得分:2)

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

<!doctype html>
<html>
<head>
  <script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script type="application/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="https://use.fontawesome.com/937a319e2f.js"></script>
  <script type="application/javascript" src="/main.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
  <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">  
  <link rel="stylesheet" type="text/css" href="/main.css">
</head>
<body>
  <!-- Table -->
  <table id="example"></table>
  <!-- Modal -->
		<div class="modal fade" id="editform" tabindex="-1" role="dialog">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title">Row details</h5>
					</div>
					<div class="modal-body">
						<form>
						  <div class="form-group"><label>Name:</label><input data-src="name" class="form-control"></input></div>
						  <div class="form-group"><label>Title:</label><input data-src="title" class="form-control"></input></div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-outline-dark" data-dismiss="modal">Close</button>
						<button type="button" class="btn btn-outline-dark" id="submitedits">Save changes</button>
					</div>
				</div>
			</div>
		</div>
</body>
</html>

您所遇到的问题是您创建的路径不正确,因为您必须使用绝对路径,而不是您所使用的相对路径。考虑上述解决方案是:

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

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

    def load_pdf(self, pdf):
        self.load(
            QtCore.QUrl.fromUserInput("%s?file=%s" % (PdfReport.PDFJS, pdf))
        )
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_())