Reputation: 145
As the title implies my goal is to change default cursor mode (insert character) to overwrite mode.
I have a validator attached to QLineEdit object
expression = QtCore.QRegExp(r'(^[A-Z0-9_]{4})-([A-Z0-9_]{6})-([A-Z0-9_]{6})-([A-Z0-9_]{4})-([A-Z0-9_]{2})-([A-Z0-9_]{6})')
object = QtGui.QRegExpValidator(expression, self)
and default value of QLineEdit component is:
object.setText('____-______-______-____-__-______')
When entering the QLineObject and in order to write anything I have to select a underscore, change it for desired character, select another one and so on. Alternatively I can select entire string, delete it and then write complete string. The point is however that sometimes I have to change some string on different position and leave unchanged underscores intact. Selecting and changing character after character is workable but tedious. Changing cursor mode from insert to overwrite when cursor focus is on one object would be much cleaner solution.
Upvotes: 0
Views: 1285
Reputation: 5815
QLineEdit does not have an overwrite mode. But QTextEdit and QPlainTextEdit have overwriteMode and setOverwriteMode(bool). If you can use a QTextEdit, you could subclass it and have your own keyReleaseEvent. Here's a quick example to test overwrite mode. Every time the user presses the Insert key on the keyboard, overwriteMode is toggled.
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit)
from PyQt5 import QtCore
import sys
class OverwriteTextBox(QTextEdit):
def __init__(self, parent=None):
super(OverwriteTextBox, self).__init__(parent=parent)
def keyReleaseEvent(self, event):
key = event.key()
if (key == QtCore.Qt.Key_Insert):
print('insert')
self.setOverwriteMode(not self.overwriteMode())
else:
# release keyrelease for normal behaviors
super().keyReleaseEvent(event)
class Example(QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent=parent)
self.setGeometry(300, 200, 570, 450)
self.setWindowTitle("Overwrite test")
self.textbox = OverwriteTextBox(parent=self)
self.textbox.setGeometry(50, 50, 200, 50)
app = QApplication(sys.argv)
example = Example()
example.show()
sys.exit(app.exec_() )
Upvotes: 2
Reputation: 145
Just for reference. The solution proposed by musicamante seems to work very well. There is only one modification. Provided the regex expression used by QRegExpValidator is the following:
expression = QtCore.QRegExp(r'(^[A-Z0-9_]{4})-([A-Z0-9_]{6})-([A-Z0-9_]{6})-([A-Z0-9_]{4})-([A-Z0-9_]{2})-([A-Z0-9_]{6})')
The input mask should use space as the character used for empty characters
object.setInputMask('>XXXX-XXXXXX-XXXXXX-XXXX-XX-XXXXXX; ')
With underscore as one of character in regex string and replacement of empties in InputMask I have experienced some strange results like this
instead of expected
Anyway once again thank for quick help.
Upvotes: 0
Reputation: 48335
Seems like you need to set the inputMask()
property.
In your case, it should be:
object.setInputMask(
'>NNNN-NNNNNN-NNNNNN-NNNN-NN-NNNNNN;_'
The N
mask character is for alphanumeric characters, when preceded by >
it means all following letters will be uppercase. Any character set after the semicolon is used as a placeholder.
Upvotes: 1