Python PyQt4:QtWebKit使用Qt资源系统显示本地文件

Python PyQt4:QtWebKit使用Qt资源系统显示本地文件,python,url,resources,pyqt4,local,Python,Url,Resources,Pyqt4,Local,如何使用Qt资源系统显示本地html文件?明显的QtCore.QUrl.fromLocalFile(“:/local\u file.html”)语法似乎不正确 文件mainwindow.qrc(编译前) 文件webbrower.py from ui_mainwindow import Ui_MainWindow import mainwindow_rc class MainWindow(QtGui.QMainWindow, Ui_MainWindow): def __init__(se

如何使用Qt资源系统显示本地html文件?明显的
QtCore.QUrl.fromLocalFile(“:/local\u file.html”)
语法似乎不正确

文件mainwindow.qrc(编译前)

文件webbrower.py

from ui_mainwindow import Ui_MainWindow
import mainwindow_rc

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setupUi(self)
        #...
        stream = QtCore.QFile(':/webbrowser_html/program_index.html')
        if stream.open(QtCore.QFile.ReadOnly):
            home_html = QtCore.QString.fromUtf8(stream.readAll())
            self.WebBrowser.setHtml()
            stream.close()

可以使用
QFile
打开本地资源文件:

stream = QFile(':/local_file.html')
if stream.open(QFile.ReadOnly):
    self.browser.setHtml(QString.fromUtf8(stream.readAll()))
    stream.close()

QUrl
需要一个方案,对于资源,它是
qrc://
。有关部分来自:

默认情况下,应用程序中的资源可以在同一目录下访问 源代码树中的文件名,前缀为
:/
,或 带有
qrc
方案的URL

例如,文件路径
:/images/cut.png
或URL
qrc:///images/cut.png
将允许访问cut.png文件,该文件 应用程序源代码树中的位置是
images/cut.png

所以,用这个来代替:

QtCore.QUrl("qrc:///local_file.html")
编辑

您正在为文件提供一个别名(
alias=“html\u home”
):

在你的情况下:

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setupUi(self)
        #...
        self.WebBrowser.load(QtCore.QUrl('qrc:///html_home'))

(如果您打算使用,您也应该调整。另外请注意,您没有在粘贴中设置页面的HTML。)

@XianJacobs:您可以发布您如何使用这些解决方案吗?@X.Jacobs。您的示例代码没有将
home\u html
传递到
self.WebBrowser.setHtml()
QtCore.QUrl("qrc:///local_file.html")
<qresource prefix="/">
    <file alias="html_home">webbrowser_html/program_index.html</file>
QtCore.QUrl("qrc:///html_home")
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setupUi(self)
        #...
        self.WebBrowser.load(QtCore.QUrl('qrc:///html_home'))