我有一个文本编辑器,可以将拉丁语键盘按键转换为俄语字符。我重新实现了一个QTextEdit类:
class MyTextEdit(QTextEdit):
def __init__(self, *args):
QTextEdit.__init__(self, *args)
leftMousePressedSignal = pyqtSignal(QPoint)
rightMousePressedSignal = pyqtSignal(QPoint, QEvent)
mouseMovedSignal = pyqtSignal(QPoint)
mouseDoubleClickedSignal = pyqtSignal(QPoint)
keyPressedSignal = pyqtSignal(QEvent)
def mousePressEvent(self, event):
pos = event.pos()
if event.button() == Qt.LeftButton:
self.leftMousePressedSignal.emit(pos)
elif event.button() == Qt.RightButton:
self.rightMousePressedSignal.emit(pos, event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
pos = event.pos()
self.mouseMovedSignal.emit(pos)
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.LeftButton:
pos = event.pos()
self.mouseDoubleClickedSignal.emit(pos)
def keyPressEvent(self, event):
if event.type() == QEvent.KeyPress:
self.keyPressedSignal.emit(event)
然后我使用重新实现的keyPressEvent。所以我不得不重新实现Backspace操作:
self.textEdit = MyTextEdit(self)
...
self.textEdit.keyPressedSignal.connect(self.OnKeyPressed)
self.actionSelectAll.triggered.connect(self.textEdit.selectAll)
...
def OnKeyPressed(self, event):
key = event.key()
txt = str(event.text())
if key == Qt.Key_Backspace:
if self.cursor.hasSelection():
self.cursor.movePosition(QTextCursor.NoMove, QTextCursor.KeepAnchor, self.cursor.selectionStart() - self.cursor.selectionStart())
else:
self.cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor, 1)
self.textEdit.setTextCursor(self.cursor)
self.textEdit.cut()
elif key == Qt.Key_A and (event.modifiers() & Qt.ControlModifier):
self.textEdit.selectAll()
现在,如果没有选择任何内容并按Backspace,它会删除光标左侧的单个字符。当用鼠标选择一个单词并按Backspace时,它会删除该单词。当用鼠标选择几个单词或整个文本并按Backspace时,它会删除整个选择。所以,它工作正常。当我按下全选按钮(或Ctrl + A)时 - 它会选择整个文本。但是如果我按Backspace它只删除光标左边的1个字符,而不是整个文本。
如果你冷酷地告诉我这里我做错了什么,我将非常感激。谢谢。
答案 0 :(得分:0)
为什么要重新选择和删除行为,以及为什么不在MyTextEdit
中完成?如果你只是需要
将拉丁语键盘按键转换为俄语字符
您只能执行此转换,并让其他工作(选择,退出,复制等)QTextEdit
。
我会这样写:
class TransliterationEdit(QTextEdit):
latin_alphabet = unicode(string.ascii_letters) # or dict with transliteration table
def translateLetter(self, letter):
return u'Ъ' # just example, you need translate letter somehow
def keyPressEvent(self, event):
text = unicode(event.text())
if text and text in self.latin_alphabet:
self.insertPlainText(self.translateLetter(text))
event.ignore()
else:
QTextEdit.keyPressEvent(self, event)
<强>更新强>
这是我TextEdit
的粗略实现,它将拉丁语按键转换为俄语字符,希望它有所帮助。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from transliterator import SMTransliterator
# transliterator available at https://gist.github.com/1600988
class TransliterationEdit(QTextEdit):
def __init__(self, *args, **kwargs):
QTextEdit.__init__(self, *args, **kwargs)
self._transliterator = SMTransliterator()
def keyPressEvent(self, event):
text = unicode(event.text())
if not self._transliterator.add_letter(text):
if text and self._transliterator.result:
self.insertPlainText(self._transliterator.result)
return QTextEdit.keyPressEvent(self, event)
if self._transliterator.need_next:
return # skip it
self.insertPlainText(self._transliterator.result)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = TransliterationEdit()
window.show()
sys.exit(app.exec_())
音译器位于gist