2012-12-16 30 views
1

我試圖創建一個循環,但它不會等待loadFinished信號:循環幫助:它不會等待loadFinished SIGNAL PyQt4中

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地址。 從瀏覽器(),他們傳遞給NetworkAccessManager在那裏我做:

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

的問題是:當我的文件只有1個IP地址,它工作正常!但是,如果它們中有幾個,那麼它比控制檯中的輸出更多,它會遍歷for循環,並且不會等到Browser()類中的load完成。

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信號這麼簡單:

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


    def _loadFinished(self): 
     print "Done" 
+0

你在哪裏te讓你的循環等待'loadFinished'? – 2012-12-17 10:37:42

+0

你能舉一個例子說明如何等待嗎? – Vor

+0

結帳my [answer](http://stackoverflow.com/a/13914370/1006989) – 2012-12-17 12:42:29

回答

1

調用br.load(url)之前試試下面的代碼:

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

這是假設你的Browser發出loadFinished(這是一個子類QWebView

+0

很多感謝,這正是我一直在尋找 – Vor