2011-05-07 57 views
4

我不知道关于Qt的第一件事,但我试图做一个厚脸皮,并从别处借用代码(http://lateral.netmanagers.com.ar/weblog/posts/BB901.html#disqus_thread)。 ;)如何正确关闭PyQt的QtApplication?

我有一个问题。当我第一次运行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) 
+0

所以你想通过复制他人的工作来编写代码,而你却不明白你到底在拷贝什么? – dassouki 2011-05-07 23:02:01

+0

当为特定目的专门提供代码时,借用GPL代码来处理小脚本是非常合理的。这与使用库非常相似,只是库不起作用,我试图找出如何解决它。 – 2011-05-07 23:30:13

+0

我跑了它,它工作正常。 (在添加进口后,你遗漏了。) – Nathan 2011-05-08 00:26:27

回答

1

QApplication应该只能初始化一次! 只要你愿意,它可以被尽可能多的Capture实例使用,但你应该在主循环中启动它们。 请参阅:https://doc.qt.io/qt-4.8/qapplication.html

您也可以在“app.exec_”之后尝试“del app”,但我不确定结果。 (你原来的代码运行正常我的系统上)

我会转而使用的WebKit的urllib:

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,否则使用全局的东西。

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

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

+0

我试图多次调用GUI时遇到了这个问题。解决方案是只创建一次QApplication并将其传递给GUI启动。 – derchambers 2015-11-19 23:38:28