Get plain text from QLineEdit

我的梦境 提交于 2019-11-27 08:12:53

问题


I want to retrieve plain text from QLineEdit() object. The text method returns a QString object. I just want a simple string object. I am using pyqt4.

def n(self):
    new_label=QLineEdit()
    new_label.setText("txt")
    txt=self.new_label.text()
    self.name=txt

txt should be a simple string not QString.


回答1:


To convert one QString in Python 2, do this:

    self.name = unicode(self.new_label.text())

To automatically convert all QStrings, put this at the beginning of your code:

import sip
sip.setapi('QString', 2)
# must be before any pyqt imports

from PyQt4 import QtCore, QtGui

If you do this, there's no need to keep using unicode(), because all methods will return python strings instead of QStrings. And note that with Python 3, this behaviour is the default, so you would not need to do anything to always get python strings.




回答2:


Convert to string with:

self.name = str(txt)


来源:https://stackoverflow.com/questions/30482250/get-plain-text-from-qlineedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!