在 QLineEdit 中使用 title() 问题?
Issue in QLineEdit with title()?
在我的程序中。使用两个 QLineEdit。第一个是正常的,第二个是 titled Line edit。第一个 one/normal QLineEidt 工作顺利,但是在第二个文本框 (QLineEdit) 中,我 无法在请求或任何地方一次插入文本 。
例如:我输入了一个文本"Python"。现在我将 "Hello" 添加到文本的开头 ("Hello Python")。如果我尝试键入 "Hello",我一次只能插入一个单词,(按 home 键,键入单词 "H",之后光标跳到末尾,我们再次将光标移动到第二个位置,然后输入一个单词 "O",一旦我们输入单词 "O" 光标跳到文本的末尾等等)。我想一口气输入(插入)一段文字。
如何克服?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
class Lineedit_title(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100,100,500,500)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(50,50,200,50)
self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(50,140,200,50)
self.textbox2.setFont(QFont("Caliber",15,QFont.Bold))
self.textbox2.textChanged.connect(self.textbox_textchange)
def textbox_textchange(self,txt):
self.textbox2.setText(txt.title())
def main():
app = QApplication(sys.argv)
win = Lineedit_title()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
问题是当你使用setText()
时,光标位置设置在文本的末尾,在这种情况下,光标位置必须分别在setText()
之前和之后保存和恢复:
def textbox_textchange(self, txt):
<b>position = self.textbox2.cursorPosition()</b>
self.textbox2.setText(txt.title())
<b>self.textbox2.setCursorPosition(position)</b>
更优雅的解决方案是使用自定义验证器:
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget
from PyQt5.QtGui import QFont, QValidator
class TitleValidator(QValidator):
def validate(self, text, pos):
return QValidator.Acceptable, text.title(), pos
class Lineedit_title(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(50, 50, 200, 50)
self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(50, 140, 200, 50)
self.textbox2.setFont(QFont("Caliber", 15, QFont.Bold))
validator = TitleValidator(self.textbox2)
self.textbox2.setValidator(validator)
def main():
app = QApplication(sys.argv)
win = Lineedit_title()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
在我的程序中。使用两个 QLineEdit。第一个是正常的,第二个是 titled Line edit。第一个 one/normal QLineEidt 工作顺利,但是在第二个文本框 (QLineEdit) 中,我 无法在请求或任何地方一次插入文本 。
例如:我输入了一个文本"Python"。现在我将 "Hello" 添加到文本的开头 ("Hello Python")。如果我尝试键入 "Hello",我一次只能插入一个单词,(按 home 键,键入单词 "H",之后光标跳到末尾,我们再次将光标移动到第二个位置,然后输入一个单词 "O",一旦我们输入单词 "O" 光标跳到文本的末尾等等)。我想一口气输入(插入)一段文字。
如何克服?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
class Lineedit_title(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100,100,500,500)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(50,50,200,50)
self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(50,140,200,50)
self.textbox2.setFont(QFont("Caliber",15,QFont.Bold))
self.textbox2.textChanged.connect(self.textbox_textchange)
def textbox_textchange(self,txt):
self.textbox2.setText(txt.title())
def main():
app = QApplication(sys.argv)
win = Lineedit_title()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
问题是当你使用setText()
时,光标位置设置在文本的末尾,在这种情况下,光标位置必须分别在setText()
之前和之后保存和恢复:
def textbox_textchange(self, txt):
<b>position = self.textbox2.cursorPosition()</b>
self.textbox2.setText(txt.title())
<b>self.textbox2.setCursorPosition(position)</b>
更优雅的解决方案是使用自定义验证器:
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget
from PyQt5.QtGui import QFont, QValidator
class TitleValidator(QValidator):
def validate(self, text, pos):
return QValidator.Acceptable, text.title(), pos
class Lineedit_title(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(50, 50, 200, 50)
self.textbox1.setFont(QFont("Caliber", 15, QFont.Bold))
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(50, 140, 200, 50)
self.textbox2.setFont(QFont("Caliber", 15, QFont.Bold))
validator = TitleValidator(self.textbox2)
self.textbox2.setValidator(validator)
def main():
app = QApplication(sys.argv)
win = Lineedit_title()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()