2

From QLineEdit, if I press :

  1. Ctrl + S, it works fine

  2. But at the same time, if I press Alt + C, or Shift+S (as per my code)

Event filter works fine, but at the same time QLineEdit Box Updated with that Pressing key.

For example if I press Alt+C from QLineEdit, the Letter "C" updated/appeared in my QLineEdit and Press Shift+S, In QLineEdit, the letter "S" is updated/appeared.

How to avoid it ?

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class textbox_keypress(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("List Box Example")
        self.mydesign()
    # ----------------------------------------------------------------------------------
    def mydesign(self):
        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(10,10,300,30)
        self.textbox.installEventFilter(self)


    def eventFilter(self, source, event):
        if event.type() == QEvent.KeyPress and source is self.textbox:
            if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_S:
                print("Control + S")
            if event.key() == Qt.Key_C and event.modifiers() == Qt.AltModifier:
                print("Alt + C")
            if event.key() == Qt.Key_E and event.modifiers() == Qt.ShiftModifier:
                print("Shift + E ")
        return super(textbox_keypress, self).eventFilter(source,event)

    # ----------------------------------------------------------------------------------
def main():
    myapp = QApplication(sys.argv)
    mywindow = textbox_keypress()
    mywindow.show()
    sys.exit(myapp.exec_())
if __name__ =="__main__":
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Bala
  • 648
  • 5
  • 15

1 Answers1

2

If you want the text not to appear in the QLineEdit then you have to make the event not send to the QLineEdit, and in the case of the event filter, just return True, for example:

# ...
if event.key() == Qt.Key_C and event.modifiers() == Qt.AltModifier:
    print("Alt + C")
    return True
# ...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241