AttributeError:"QString"对象没有属性"rfind" [英] AttributeError: 'QString' object has no attribute 'rfind'

查看:115
本文介绍了AttributeError:"QString"对象没有属性"rfind"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在一台计算机上运行良好,但是当我在另一台计算机上运行相同的应用程序时,出现错误:

My application runs fine in one computer but when I run the same application in another I get the error:

Traceback (most recent call last):
  File "./th.py", line 98, in browse_file2
    self.textEdit_2.append(str(os.path.basename(p)))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 121, in basename
    i = p.rfind('/') + 1
AttributeError: 'QString' object has no attribute 'rfind'

我在此处看到了类似的错误.据此,我需要将参数类型转换为'QString'数据类型,而我正在这样做.脚本中有错误的部分是:

I have seen a similar error here. According to this, I need to typecast parameter to 'QString' datatype and I am doing that.The part of script that is having error is:

def browse_file(self):
    #files handling


    caption="Open File"
    directory='./'
    filter_mask="fastq files (*.fastq)"
    self.textEdit.setText("")
    f_1=(QFileDialog.getOpenFileNames(None, caption, directory, filter_mask))
    #for st in f_1:
    for p in f_1:
     self.textEdit.append(str(os.path.basename(p)))
    global R1
    R1=f_1

        #if textEdit.toPlainText


def browse_file2(self):
    #files handling
    caption="Open File"
    directory='./'
    filter_mask="fastq files (*.fastq)"
    f_2=(QFileDialog.getOpenFileNames(None, caption, directory, filter_mask))
    for p in f_2:
     self.textEdit_2.append(str(os.path.basename(p)))
     global R2
     R2=f_2

有人可以告诉这个错误的可能原因吗?如果您需要任何其他代码部分,请告诉我.预先感谢.

Can someone please tell what may be the possible cause of this error? Let me know if you need any other part of code. Thanks in advance.

推荐答案

pyqt 首先包装 Qt 时,他们保留了 QString 类将其强制转换为本地python字符串(即 str ).大多数在字符串上运行的python库(例如 os.path )都期望使用 str unicode 对象,而不是 QString 's.这意味着您必须不断地从 pyqt

When pyqt first wrapped Qt, they kept the QString class instead of casting it to native python strings (ie. str). Most python libraries that operate on strings (like os.path) expect str or unicode objects, not QString's. This means that you constantly have to type-cast all the return values from pyqt

text = unicode(mywidget.text())

幸运的是, pyqt 具有较新版本的api,它们会自动为您进行类型转换.您只需要告诉它使用更新的api即可.在python代码的开头,执行其他任何导入之前,您可以先执行此操作

Fortunately, pyqt has newer versions of the api that automatically do the type-casting for you. You just need to tell it to use the newer api. At the beginning of your python code, before you do any other imports, you can do this

import sip
sip.setapi('QString', 2)

也有更新的api ,用于许多对象.

这篇关于AttributeError:"QString"对象没有属性"rfind"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆