QLineEdit和自定义功能不兼容

时间:2012-06-20 14:09:28

标签: python string pyqt4

我试图用Python和PyQt4完成我的项目,并且我在通过我做的函数传递QLineEdit变量时遇到了问题。该字符串应该作为一个url工作,当我通过我的第一个参数传递它,它试图读取url并获取其内容时,它会抛出这个错误:

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问题还是我的功能有问题?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您没有发布足够的代码来证明您的陈述

  

字符串应该作为网址工作,当我通过我的第一个参数

传递它时

看起来你正在将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

答案 1 :(得分:0)

缺少self.a

  

http:// ”或“ https://

download_mango("http://"+self.a,self.path2)

请参阅http://www.nullege.com/codes/search/urllib2.Request.get_type