循环帮助:它不等待PyQt4中的loadFinished SIGNAL

时间:2012-12-16 20:02:06

标签: python qt for-loop pyqt pyside

我正在尝试创建一个循环,但它不会等待loadFinished信号:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    for line in open('proxy_list_test.txt', 'r').readlines():
        network_data = line.split(':')
        ip = network_data[0]
        port = network_data[1]
        print "Connecting with ip: %s port: %s"%(ip,port)
        br = Browser(ip, port)
        url = QUrl('http://example web site.com/')
        br.load(url)
        br.show()
        print "Closing"
    app.exec_()

正如您所看到的,我正在尝试使用几个不同的IP地址加载示例网站。 从Browser()他们传递给我做的NetworkAccessManager:

proxy = QNetworkProxy(QNetworkProxy.HttpProxy, self.ip, self.port)
self.setProxy(proxy)

问题是:当我的文件只有1个IP地址时,它工作正常!但如果它有几个,而不是控制台中的输出,它会通过for循环,并且不会等到在Browser()类中加载完成。

Connecting with ip: some_ip port: some_port

10%
Closing
Connecting with ip: some_ip port: some_port

10%
Closing
Connecting with ip: some_ip port: some_port

10%
Closing
Done
100%

和loadFinished和loadProgress SIGNALS很简单:

def _progress(self, progress):
        print str(progress) + "%"


    def _loadFinished(self):
        print "Done"

1 个答案:

答案 0 :(得分:1)

在致电br.load(url)之前尝试此代码:

loop = QEventLoop()
br.loadFinished.connect(loop.quit)
loop.exec_() 

这假设您Browser发出loadFinished(它是QWebView的子类)