【问题标题】:Using QApplication::exec() in a function or class在函数或类中使用 QApplication::exec()
【发布时间】:2016-12-12 12:55:05
【问题描述】:

我希望下面的代码在web加载过程中获取以http://down.51en.com:88开头的请求url,然后用url的响应对象做其他处理。 在我的程序中,一旦为targetUrl 赋值,我希望函数targetUrlGetter(url) 将其返回给调用者,但是,问题是QApplication::exec() 进入主事件循环,因此无法在结束时执行代码targetUrlGetter() exec() 调用后的函数,因此函数无法返回,我尝试在interceptRequest(self, info) 中使用qApp.quit() 以告诉应用程序退出以便targetUrlGetter(url) 可以返回,但函数仍然无法返回并且程序甚至在退出时崩溃(在Win7 32位上测试),那么我怎样才能将targetUrl返回给调用程序?

这里的难点是如何在不崩溃的情况下退出Qt事件循环并将请求url返回给调用者。

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import *


class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = parent

    def interceptRequest(self, info):
        if info.requestUrl().toString().startswith('http://down.51en.com:88'):
            self.targetUrl = info.requestUrl().toString()
            print('----------------------------------------------', self.targetUrl)
            qApp.quit()

            # self.page.load(QUrl(''))


def targetUrlGetter(url=None):
    app = QApplication(sys.argv)
    page = QWebEnginePage()
    globalSettings = page.settings().globalSettings()
    globalSettings.setAttribute(
        QWebEngineSettings.PluginsEnabled, True)
    globalSettings.setAttribute(
        QWebEngineSettings.AutoLoadImages, False)
    profile = page.profile()
    webEngineUrlRequestInterceptor = WebEngineUrlRequestInterceptor(page)
    profile.setRequestInterceptor(webEngineUrlRequestInterceptor)
    page.load(QUrl(url))
    # view = QWebEngineView()
    # view.setPage(page)
    # view.show()
    app.exec_()
    return webEngineUrlRequestInterceptor.targetUrl


url = "http://www.51en.com/news/sci/everything-there-is-20160513.html"
# url = "http://www.51en.com/news/sci/obese-dad-s-sperm-may-influence-offsprin.html"
# url = "http://www.51en.com/news/sci/mars-surface-glass-could-hold-ancient-fo.html"
targetUrl = targetUrlGetter(url)
print(targetUrl)

【问题讨论】:

  • 我无法重现此内容。该脚本运行没有错误,并按预期打印/返回请求的 url。我看不出exec() 应该“挂起”的任何理由。你确定没有网络问题?
  • @ekhumoro 对不起我糟糕的英语,再次查看更新后的帖子!如果您看不到退出崩溃,请在 Windows 上多次测试代码
  • 我无法在 Windows 上进行测试。该脚本在 Linux 上运行良好。您需要进行一些基本的调试以查看脚本失败的地方。网页是否正确加载?使用loadFinished signal 进行检查。 interceptRequest 是否曾经被调用,如果是,print(repr(info.requestUrl().toString())) 的输出是什么? if 语句是否曾经评估为 True
  • @ekhumoro 一切正常,除了退出崩溃
  • 尝试为app 和/或page 使用全局变量。可能存在垃圾收集问题(即对象以错误的顺序被删除)。

标签: python qt pyqt qt5 pyqt5


【解决方案1】:

您应该始终在程序开始时初始化QApplication,并始终在程序结束时调用QApplication::exec 函数。

另一件事是QWebEngineUrlRequestInterceptor.interceptRequest 是一个异步调用的回调函数。由于info.requestUrl().toString()是在回调函数内部调用的,所以没有办法return的结果同步。

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import *


class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = parent

    def interceptRequest(self, info):
        if info.requestUrl().toString().startswith('http://down.51en.com:88'):
            self.targetUrl = info.requestUrl().toString()
            print('----------------------------------------------', self.targetUrl)
            # Don't do thatDo you really want to exit the whole program?
            # qApp.quit()

            # Do something here, rather than return it.
            # It must be run asynchronously

            # self.page.load(QUrl(''))


def targetUrlGetter(url=None):
    page = QWebEnginePage()
    globalSettings = page.settings().globalSettings()
    globalSettings.setAttribute(
        QWebEngineSettings.PluginsEnabled, True)
    globalSettings.setAttribute(
        QWebEngineSettings.AutoLoadImages, False)
    profile = page.profile()
    webEngineUrlRequestInterceptor = WebEngineUrlRequestInterceptor(page)
    profile.setRequestInterceptor(webEngineUrlRequestInterceptor)
    page.load(QUrl(url))
    # view = QWebEngineView()
    # view.setPage(page)
    # view.show()

    # Don't return this. It cannot be called synchronously. It must be called asynchronously.
    # return webEngineUrlRequestInterceptor.targetUrl

app = QApplication(sys.argv)  # always initialize QApplication at the beginning of the program
url = "http://www.51en.com/news/sci/everything-there-is-20160513.html"
# url = "http://www.51en.com/news/sci/obese-dad-s-sperm-may-influence-offsprin.html"
# url = "http://www.51en.com/news/sci/mars-surface-glass-could-hold-ancient-fo.html"
targetUrl = targetUrlGetter(url)
print(targetUrl)
app.exec_() # always call the QApplication::exec at the end of the program

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 2018-02-27
    • 2016-11-29
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多