如何正确关闭PyQt的QtApplication?

4 投票
2 回答
2219 浏览
提问于 2025-04-16 17:12

我对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)

2 个回答

1

一个 QApplication 只需要初始化一次!

你可以在多个 Capture 实例中使用它,但要确保在主循环中启动它们。

可以参考这个链接了解更多:https://doc.qt.io/qt-4.8/qapplication.html

你也可以在 "app.exec_" 后试试 "del app",但我不太确定这样做的结果会怎样。

(你原来的代码在我的系统上运行得很好)

我建议使用 urllib 而不是 webkit:

import urllib

class Capturer:
    def capture(self, s_url, s_filename):
        s_file_out, httpmessage = urllib.urlretrieve(s_url, s_filename, self.report)

    def report(self, i_count, i_chunk, i_size):
        print('retrived %5d of %5d bytes' % (i_count * i_chunk, i_size))

def test():
    c = Capturer()
    c.capture("http://www.google.com/google.png", "google1.png")
    c.capture("http://www.google.com/google.png", "google2.png")

if __name__ == '__main__':
    test()
2

你需要在测试函数之外处理QApplication,就像使用单例模式一样(在这里其实是合适的)。

你可以检查一下QtCore.qApp是否存在(或者检查QApplication.instance()是否返回None或其他东西),只有在这种情况下才创建你的qApp,否则就使用全局的那个。

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

如果你想确保它被正确处理,可以设置一个懒加载的单例来管理它。

撰写回答