【问题标题】:Text edit always uppercase文本编辑总是大写
【发布时间】:2015-01-03 12:47:04
【问题描述】:

我正在使用 PySide 编写一个编剧应用程序。我想要的是在用户输入时将字符转换为大写。

每次添加字符时,以下代码都会出现运行时错误,提示“超出最大递归深度”。我明白这意味着什么以及为什么会发生,但有不同的方法吗?

self.cursor = self.recipient.textCursor()
self.cursor.movePosition(QTextCursor.StartOfLine)
self.cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
self.curtext = self.cursor.selectedText()
if len(self.curtext) > len(self.prevText):
    self.cursor.insertText(self.curtext.upper())
    self.cursor.clearSelection()
self.prevText = self.curtext

只要文本编辑小部件中的文本发生更改,上面的代码就会运行。 if 语句阻止代码在用户没有插入文本时运行。

【问题讨论】:

    标签: python qt pyside uppercase qtextedit


    【解决方案1】:

    您收到递归错误可能是因为在将输入修复为大写时,您正在更改内容并再次触发相同的修复例程。此外,您不断地更改整条生产线,而只有一部分发生了变化并需要修复。

    幸运的是,Qt 可以使用QTextCharFormat 自己完成此操作。下面是自动将所有文本保留在 QLineEdit 大写字母中的示例。你还可以做更多with it,比如加下划线或加粗文本...

    例子:

    from PySide import QtGui
    
    app = QtGui.QApplication([])
    
    widget = QtGui.QTextEdit()
    fmt = QtGui.QTextCharFormat()
    fmt.setFontCapitalization(QtGui.QFont.AllUppercase)
    widget.setCurrentCharFormat(fmt)
    widget.show()
    
    app.exec_()
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多