I just wonder how to make a QLineEdit
clickable because I want when the QLineEdit
is clicked to clear the line's text.
Asked
Active
Viewed 2,652 times
1

Mel
- 5,837
- 10
- 37
- 42

Leontis Kot
- 11
- 1
- 2
-
Possible duplicate of [Can QWidget detect mouse events on behalf of a QLineEdit?](http://stackoverflow.com/questions/13465329/can-qwidget-detect-mouse-events-on-behalf-of-a-qlineedit) – Mel Jan 28 '16 at 08:33
-
1Possible duplicate of [How to get Click Event of QLineEdit in QT?](http://stackoverflow.com/questions/6452077/how-to-get-click-event-of-qlineedit-in-qt) – Bob Jan 28 '16 at 17:13
-
It is quite a trivial thing to do. If all you need is clearing the line edit, reimplment `mousePressEvent` or `mouseReleaseEvent` in your custom class and clear the text. Or use event filters as in the link given by @Ian above. – Marcus Jan 31 '16 at 14:16
-
Use [setClearButtonEnabled](https://doc.qt.io/qt-5/qlineedit.html#clearButtonEnabled-prop). – ekhumoro Feb 20 '17 at 18:29
2 Answers
0
Try Below Code to make QLineEdit
clickable :
class ClickableLabel(QLabel):
clicked = pyqtSignal()
def __init__(self,name, widget):
super().__init__(name, widget)
def mousePressEvent(self, QMouseEvent):
self.clicked.emit()

LuFFy
- 8,799
- 10
- 41
- 59

Cvetan Tomov
- 1
- 1
0
Here is my 2 cents...
Definition :
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal
class cQLineEdit(QLineEdit):
clicked= pyqtSignal()
def __init__(self,widget):
super().__init__(widget)
def mousePressEvent(self,QMouseEvent):
self.clicked.emit()
usage :
self.cLE = cQLineEdit(self)
self.cLE.setFixedWidth(20)
self.cLE.move(10,200)
self.cLE.clicked.connect(self.printText)
def printText(self):
print("Yop,+++")
Hope this can help.