QWebView不加载外部资源

发布于 2024-08-22 03:05:07 字数 986 浏览 3 评论 0原文

我正在使用 Qt 和 PyQt4 开发一个 kiosk Web 浏览器。除了一个怪癖之外,QWebView 似乎工作得很好。

如果 URL 由于任何原因无法加载,我想将用户重定向到自定义错误页面。我已经使用 loadFinished() 信号来检查结果,并在必要时使用 QWebView.load() 更改自定义页面的 URL。但是,我尝试在此处加载的任何页面都无法提取 CSS 或图像等外部资源。

使用 QWebView.load() 在启动时设置初始页面似乎工作正常,并且单击自定义错误页面上的任何链接将导致目标页面加载正常。只是错误页面不起作用。

我真的不知道下一步该去哪里。我已经包含了一个应用程序的源代码,该应用程序将复制下面的问题。它采用 URL 作为命令行参数 - 有效的 URL 将正确显示,错误的 URL(例如 DNS 解析失败)将重定向到 Google,但徽标丢失。

import sys from PyQt4 import QtGui, QtCore, QtWebKit

class MyWebView(QtWebKit.QWebView):
 def __init__(self, parent=None):
  QtWebKit.QWebView.__init__(self, parent)
  self.resize(800, 600)
  self.load(QtCore.QUrl(sys.argv[1]))
  self.connect(self, QtCore.SIGNAL('loadFinished(bool)'), self.checkLoadResult)

 def checkLoadResult(self, result):
  if (result == False):
   self.load(QtCore.QUrl('http://google.com'))

app = QtGui.QApplication(sys.argv)
main = MyWebView()
main.show()
sys.exit(app.exec_())

如果有人可以提供一些建议,我们将不胜感激。

I'm working on a kiosk web browser using Qt and PyQt4. QWebView seems to work quite well except for one quirk.

If a URL fails to load for any reason, I want to redirect the user to a custom error page. I've done this using the loadFinished() signal to check the result, and change the URL to the custom page if necessary using QWebView.load(). However, any page I attempt to load here fails to pull in external resources like CSS or images.

Using QWebView.load() to set the initial page at startup seems to work fine, and clicking any link on the custom error page will result in the destination page loading fine. It's just the error page that doesn't work.

I'm really not sure where to go next. I've included the source for an app that will replicate the problem below. It takes a URL as a command line argument - a valid URL will display correctly, a bad URL (eg. DNS resolution fails) will redirect to Google, but with the logo missing.

import sys from PyQt4 import QtGui, QtCore, QtWebKit

class MyWebView(QtWebKit.QWebView):
 def __init__(self, parent=None):
  QtWebKit.QWebView.__init__(self, parent)
  self.resize(800, 600)
  self.load(QtCore.QUrl(sys.argv[1]))
  self.connect(self, QtCore.SIGNAL('loadFinished(bool)'), self.checkLoadResult)

 def checkLoadResult(self, result):
  if (result == False):
   self.load(QtCore.QUrl('http://google.com'))

app = QtGui.QApplication(sys.argv)
main = MyWebView()
main.show()
sys.exit(app.exec_())

If anyone could offer some advice it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2024-08-29 03:05:07

不知道为什么这不起作用,但类似的东西

 def checkLoadResult(self, result):
  if (result == False):
    self.page().mainFrame().setHtml ( "<html><head><h1>Not Found</h1></head>\
     <body><p> Search at  <a href='http://google.com'> google </a>\
     </p></body> </html>")

却可以。

Do not know why this does not work but something like

 def checkLoadResult(self, result):
  if (result == False):
    self.page().mainFrame().setHtml ( "<html><head><h1>Not Found</h1></head>\
     <body><p> Search at  <a href='http://google.com'> google </a>\
     </p></body> </html>")

does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文