在函数或类中使用QApplication :: exec()

时间:2016-08-06 04:33:39

标签: python qt pyqt qt5 pyqt5

我希望以下代码在Web加载过程中获取以http://down.51en.com:88开头的请求URL,然后使用url的响应对象进行其他处理。 在我的程序中,一旦为targetUrl分配了值,我希望函数targetUrlGetter(url)将其返回给调用者,但问题是QApplication::exec()进入主事件循环,因此无法执行exec()调用后targetUrlGetter()函数末尾的代码,因此函数无法返回,我在qApp.quit()中尝试使用interceptRequest(self, info)以告诉应用程序退出,以便targetUrlGetter(url)可以返回,但函数仍然无法返回,程序甚至在退出时崩溃(在Win7 32bit上测试),那么如何将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)

1 个答案:

答案 0 :(得分:-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