0

My aim to deactivate the checkbox, if my textbox is empty. So I try with the following code if I clear my entire textbox by the way of "select all characters" using Ctrl+A and delete it. But in my code, the first part only worked, that is Ctrl+A, If I press the Delete key that is the second part of my code is not worked, How to rectify?

   if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_A :
        print("I am inside of Cntrl+A")
        if event.key() == Qt.Key_Delete:
            print("i Am inside of key_delete")
            self.checkbox.setChecked(False)
            self.checkstatus = 0
            return True

Minimal reproducible Code

import os
from PyQt5.QtWidgets import  QCheckBox,QLineEdit,QWidget,QApplication,QVBoxLayout
from PyQt5.QtCore import QEvent,Qt

class CtrlaDel(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Key Press Event")
        self.le = QLineEdit()
        self.cb = QCheckBox()
        self.cb.setChecked(True)
        self.vbox = QVBoxLayout(self)
        self.vbox.addWidget(self.le)
        self.vbox.addWidget(self.cb)


        self.le.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QEvent.KeyPress and source is self.le:
            print("ddddsdsdsdsdsdsds")
            if event.key() == Qt.Key_Backspace or event.key() == Qt.Key_Delete and source is self.le:
                print("ddd")
                if len(self.le.text()) <= 1:
                    self.cb.setChecked(False)


            if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_A:
                print("I am inside of Ctrl+A")
                if event.key() == Qt.Key_Delete:
                    print("I am Inside of Delete")
                    self.cb.setChecked(False)
                    self.checkstatus = 0
                    return True
        return super(CtrlaDel, self).eventFilter(source, event)
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    mainwindow = CtrlaDel()
    mainwindow.show()
    sys.exit(app.exec_())
2
  • @eyllanesc - MRE attached Commented Jul 5, 2021 at 23:48
  • 1
    Qt, as any UI toolkit, is event driven. Every event is queued, and won't be processed until the previous one has been handled. Your event filter implementation doesn't work because its logic is wrong: if the key is A, how could it also be Delete in the next if block? Commented Jul 6, 2021 at 0:26

1 Answer 1

2

I do not see the need to use an eventFilter, the solution is that every time the text is changed verify that if it is an empty text then set the checkbox to false and for this the textChanged signal can be used

from PyQt5.QtWidgets import QCheckBox, QLineEdit, QWidget, QApplication, QVBoxLayout


class CtrlaDel(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Key Press Event")
        self.le = QLineEdit()
        self.cb = QCheckBox()
        self.cb.setChecked(True)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.le)
        vbox.addWidget(self.cb)

        self.le.textChanged.connect(self.handle_text_changed)
        self.handle_text_changed()

    def handle_text_changed(self):
        if not self.le.text():
            self.cb.setChecked(False)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    mainwindow = CtrlaDel()
    mainwindow.show()

    sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

2 Comments

In my Main script try this solution, but its affect some other conditions.
@tckraomuqnt I only base myself on your requirement, I will not base my answer on other conditions so your job will be to adapt my solution to your project. Bye

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.