3

Do we have onclick event handler for textboxes in PyQt5, like we have in JQuery and C# ?

If not, Is there any other way of handling clicks on textboxes ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Rajat
  • 647
  • 3
  • 10
  • 30

1 Answers1

0

Some widgets (such as QLineEdit) don't have a clicked signal out of the box. But you can make a custom widget that inherits from the widget you need, give it a clicked signal, and emit it on mousePressEvent.

class CustomLineEdit(QtWidgets.QLineEdit):

    clicked = QtCore.pyqtSignal()

    def __init__(self):
        super().__init__()

    def mousePressEvent(self, QMouseEvent):
        self.clicked.emit()

Then you can just connect the clicked signal to whatever you want.

self.line_edit = CustomLineEdit()
self.line_edit.clicked.connect(self.handle_click)
MalloyDelacroix
  • 2,193
  • 1
  • 13
  • 17
  • I replaced self.handke_click with my function name: self.line_edit = CustomLineEdit() self.line_edit.clicked.connect(self.open_file_name_dialog) but its not working. Its not even showing the textbox on the window. – Rajat Apr 18 '18 at 17:14
  • If the line edit is not showing in the window, this means you have most likely not added it to your view. You will have to add the custom line edit to your view like any other widget. – MalloyDelacroix Apr 18 '18 at 20:31
  • This is equivalent to `onmousedown`, not `onclick`. You should also call the base-class `mousePressEvent`, otherwise you will lose all the normal `QLineEdit` mouse behaviour. – ekhumoro Apr 18 '18 at 20:44