从 QLineEdit 获取纯文本 [英] Get plain text from QLineEdit
问题描述
我想从 QLineEdit()
对象中检索纯文本.text 方法返回一个 QString
对象.我只想要一个简单的字符串对象.我正在使用 pyqt4.
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 应该是一个简单的字符串,而不是 QString
.
txt should be a simple string not QString
.
推荐答案
要在 Python 2 中转换 one QString,请执行以下操作:
To convert one QString in Python 2, do this:
self.name = unicode(self.new_label.text())
要自动转换所有 QString,请将其放在代码的开头:
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
如果你这样做,就没有必要继续使用unicode()
,因为所有方法将返回python字符串而不是QStrings.请注意,对于 Python 3,此行为是默认行为,因此您无需执行任何操作即可始终获取 Python 字符串.
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.
这篇关于从 QLineEdit 获取纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!