gpt4 book ai didi

python - 如何正确关闭 PyQt 的 QtApplication?

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:22 25 4
gpt4 key购买 nike

我对 Qt 一无所知,但我试图厚颜无耻地从其他地方借用代码 (http://lateral.netmanagers.com.ar/weblog/posts/BB901.html#disqus_thread) . ;)

我有一个问题。当我第一次运行 test() 时,一切都很顺利。但是,当我第二次运行它时,出现了令人讨厌的段错误。我怀疑问题是我没有正确结束 qt 的东西。我应该对该程序进行哪些更改才能使其多次运行?提前致谢!

from PyQt4 import QtCore, QtGui, QtWebKit
import logging

logging.basicConfig(level=logging.DEBUG)

class Capturer(object):
"""A class to capture webpages as images"""

def __init__(self, url, filename, app):
self.url = url
self.app = app
self.filename = filename
self.saw_initial_layout = False
self.saw_document_complete = False

def loadFinishedSlot(self):
self.saw_document_complete = True
if self.saw_initial_layout and self.saw_document_complete:
self.doCapture()

def initialLayoutSlot(self):
self.saw_initial_layout = True
if self.saw_initial_layout and self.saw_document_complete:
self.doCapture()

def capture(self):
"""Captures url as an image to the file specified"""
self.wb = QtWebKit.QWebPage()
self.wb.mainFrame().setScrollBarPolicy(
QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff)
self.wb.mainFrame().setScrollBarPolicy(
QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff)
self.wb.loadFinished.connect(self.loadFinishedSlot)
self.wb.mainFrame().initialLayoutCompleted.connect(
self.initialLayoutSlot)
logging.debug("Load %s", self.url)
self.wb.mainFrame().load(QtCore.QUrl(self.url))

def doCapture(self):
logging.debug("Beginning capture")
self.wb.setViewportSize(self.wb.mainFrame().contentsSize())
img = QtGui.QImage(self.wb.viewportSize(), QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(img)
self.wb.mainFrame().render(painter)
painter.end()
img.save(self.filename)
self.app.quit()

def test():
"""Run a simple capture"""
app = QtGui.QApplication([])
c = Capturer("http://www.google.com", "google.png", app)
c.capture()
logging.debug("About to run exec_")
app.exec_()

DEBUG:root:Load http://www.google.com
QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration) to QNetworkConfigurationManager::configurationRemoved(QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted()
QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(bool)
QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration) to QNetworkConfigurationManager::configurationChanged(QNetworkConfiguration)

Process Python segmentation fault (this last line is comes from emacs)

最佳答案

您需要在测试函数之外处理 QApplication,有点像单例(实际上在这里很合适)。

您可以做的是检查 QtCore.qApp 是否是某种东西(或者 QApplication.instance() 是否返回 None 或其他东西),然后才创建您的 qApp,否则,使用全局的。

它不会在你的 test() 函数之后被销毁,因为 PyQt 将应用程序存储在某个地方。

如果你想确保它被正确处理,只需为它设置一个延迟初始化的单例。

关于python - 如何正确关闭 PyQt 的 QtApplication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5923845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com