我想在 Pyside 中显示一个带有 QwebView 小部件的窗口。为此,我使用了 QtCreator 生成的一些代码:
#code generated by QtCreator:
from PySide import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.webView = QtWebKit.QWebView(self.centralWidget)
self.webView.setGeometry(QtCore.QRect(10, 20, 380, 270))
self.webView.setUrl(QtCore.QUrl("file:///C:/pdf_folder/test.pdf"))
self.webView.setObjectName("webView")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
from PySide import QtWebKit
# My code:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
APP = QtGui.QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(APP.exec_())
我确定在指定路径中有一个 pdf 文件,但是当我运行我的脚本时,pdf 文件永远不会显示在我的窗口中。 你知道我做错了什么吗?
我看到了这个话题Is it possible to get QWebKit to display pdf files? ,但答案对我不起作用(在导入行中将 PyQt 更改为 Pyside 之后)。
我也看到了这个话题PDF with QWebView: missing refresh/repaint after loading ,解决方法(在加载前使用计时器)对我有用。但是我不认为使用计时器加载文件是解决问题的好方法。
而且,主要是,我用于 Ui_MainWindow
的代码是用 QtCreator 生成的,我没有更改它,我也不想自己更改它(不使用 QtCreator)。它只是一个窗口的简单形式,只有一个小部件 QtWebKit.QWebView
我想在其中加载一个文件。它应该可以在没有奇怪的解决方法的情况下工作。为什么自动生成的代码不起作用?我是否以错误的方式使用了 QtCreator?
最佳答案
你必须启用QWebView
的插件:
self.webView = QtWebKit.QWebView(self.centralWidget)
self.webView.settings().setAttribute(QWebSettings.PluginsEnabled, True)
也尝试在显示主窗口后设置 QWebView
URL。
MW = MainWindow()
MW.show()
MW.ui.webView.setUrl(QtCore.QUrl("file:///C:/pdf_folder/test.pdf"))
我认为绘画事件与您没有看到 pdf 文件这一事实有关。
关于python - 使用 QWebView 显示 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24613347/