QLineEdit how to detect every keypress?
-
I noticed there is no signal from QLineEdit for detecting single-character changes to the text field.
How can this be done?
I tried overrriding keyPressEvent(QKeyEvent *event) but that only gets called when I press Return. -
@Publicnamer keyPressEvent is the proper solution so I assume you have implemented it incorrectly, could you please show a compilable example of your intent?
-
@eyllanesc I didn't subclass QLineEdit, that's why it didn't work.
It doesn't matter though. Listening to cursorPositionChanged suffices. However I do think QLineEdit should be revised to allow for watching individual character changes without subclassing. Other GUIs allow that.
-
Does
single-character change
refer to inserting and deleting text, or something else? -
@Publicnamer It should be noted that cursorPositionChanged not only fires when you type a letter, for example it is emitted when you move the cursor by clicking with the mouse.
If you don't want to subclass QLineEdit then you can listen to keyboard events through an eventfilter. If you just want to hear some special keys then you can use QShortcuts.
The logic of a QWidget is to balance the number of behaviors with the minimum expected. If you want more behaviors then that implies a cost in memory, in addition to that you would never be happy at all. So the Qt developers implement the behaviors that they consider necessary, if you want other behaviors then that's your job.
-
@jeremy_k All of the above.
I'm looking for something like iOS's UITextViewDelegate's method textField:shouldChangeCharactersInRange:replacementString:
https://developer.apple.com/documentation/uikit/uitextfielddelegate -
@Publicnamer This component looks like QValidator: https://doc.qt.io/qt-5/qvalidator.html
-
@Publicnamer said in QLineEdit how to detect every keypress?:
@jeremy_k All of the above.
I'm looking for something like iOS's UITextViewDelegate's method textField:shouldChangeCharactersInRange:replacementString:
https://developer.apple.com/documentation/uikit/uitextfielddelegateAll of the above
in response tosomething else
doesn't clarify anything. What does QLineEdit::textChanged() or QLineEdit::textEdited() miss?