2012-06-20 81 views
0

我正試圖用Python和PyQt4完成我的項目,並且遇到了通過我所做的函數傳遞QLineEdit變量的問題。該字符串應該工作作爲一個網址,當我通過它通過我的第一個參數,它試圖讀取URL並獲得它的內容,它拋出我這個錯誤:QLineEdit和自定義函數不兼容

Traceback (most recent call last): 
    File "programa2.py", line 158, in on_link_clicked 
    download_mango(self.a, self.path2) 

    File "c:\Users\Poblet\ManGet\mango.py", line 19, in download_mango 
    urlContent = urllib2.urlopen(url).read() # We read the URL 

    File "c:\Python27\lib\urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 

    File "c:\Python27\lib\urllib2.py", line 386, in open 
    protocol = req.get_type() 

AttributeError: 'QString' object has no attribute 'get_type' 

這是由以下動作觸發:

def on_link_clicked(self): 
    self.a = self.linkEdit.displayText() 
    download_mango(self.a, self.path2) 

而我完全失去了。它可能是一個PyQt4問題或者我的函數有問題嗎?

我感謝您的幫助。

+0

請粘貼您的代碼。什麼是download_mango? – Froyo

回答

1

你沒有張貼足夠的代碼來證明你的聲明

The string should work as an url and when I pass it through my first argument

外貌裏你可以把一個QString傳遞給urlopen。只需將其包裝在str()中,您應該可以。

>>> url = QString('http://stackoverflow.com/questions/11121475') 
>>> urllib2.urlopen(url).read() 
### this generates your error ending with 
AttributeError: 'QString' object has no attribute 'get_type' 

>>> urllib2.urlopen(str(url)).read() 
### works 
+0

謝謝!我一直認爲QLineEdit已經像字符串一樣傳遞了東西。現在我不會再犯同樣的錯誤兩次了。 –

+0

我已經開始使用PySide了,因爲它刪除了「QString和其他無用的Qt東西,用於Python開發者,比如QVariant」。像許可證一樣更好。 –