pyqt QLineEdit在设置有 setInputMask 的时候,鼠标点击 QLineEdit 时候,自动定位到行首
解决问题
由于调用 setInputMask 后,导致光标不能自动定位到行首开始输入的问题
from PyQt5.Qt import *
import sys
class MyLineEdit(QLineEdit):
def mousePressEvent(self, event):
super().mousePressEvent(event) # 这个要调用需要在前面调用,否则系统设定会覆盖光标移动到行首的自定义设定
self.setCursorPosition(0)
class MyWdidget(QWidget):
def __init__(self):
super().__init__(parent=None)
form_layout = QFormLayout()
label1 = QLabel()
label1.setText('label1')
line_edit1 = MyLineEdit()
line_edit1.setInputMask('HHHHHH;_')
form_layout.addRow(label1, line_edit1)
self.setLayout(form_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
wind = MyWdidget()
wind.show()
sys.exit(app.exec())