21

How to get Click Event of QLineEdit in Qt ?

I am not able to see any SLOT related to click in QLineEdit ?

Mel
  • 5,837
  • 10
  • 37
  • 42
Bokambo
  • 4,204
  • 27
  • 79
  • 130

7 Answers7

28

I don't think subclassing a QLineEdit is the right choice. Why subclass if you don't need to? You could instead use event filters. Check out QObject::eventFilter.

Example:

MyClass::MyClass() :
    edit(new QLineEdit(this))
{
    edit->installEventFilter(this);
}

bool MyClass::eventFilter(QObject* object, QEvent* event)
{
    if(object == edit && event->type() == QEvent::FocusIn) {
        // bring up your custom edit
        return false; // lets the event continue to the edit
    }
    return false;
}
Kiruahxh
  • 1,276
  • 13
  • 30
buck
  • 1,502
  • 1
  • 20
  • 23
10

You need to reimplement focusInEvent in a new class extending QLineEdit. The following links are going to help you.

  1. http://doc.qt.io/qt-5/qwidget.html#focusInEvent
  2. QLineEdit - focus event
  3. How to know if a QLineEdit got focus?
  4. QLineEdit Focus Event
Community
  • 1
  • 1
Raj
  • 22,346
  • 14
  • 99
  • 142
5

Although there is no "clicked" or "entered" event. You can use the

void cursorPositionChanged(int old, int new)

Signal. It is emitted when the user clicks the lineedit (if it is enabled) and also on a few other occasions so you have to verify which of the events actually happened but I think this is still easier than subclassing or using the event listener for some applications.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
3

I dono if this will help, i had to call a function once a text is entered. This is how i did it.

connect(ui->passwordSetLineEdit,SIGNAL(textEdited(QString)),this,SLOT(onTextEdit(QString)));

when a text is entered textEdited signal will be emited, thus my onTextEdit function will be called.

alfah
  • 2,077
  • 1
  • 31
  • 55
1

There is no signals like clicked() for QLineEdit, but you can subclass it and emit such signal in your custom implementation of mouseReleaseEvent.

AAEM
  • 1,837
  • 2
  • 18
  • 26
Raiv
  • 5,731
  • 1
  • 33
  • 51
0

I used this solution many times

def clickable(widget): # make this function global
    class Filter(QObject):
        clicked = pyqtSignal()

        def eventFilter(self, obj, event):
            if obj == widget and event.type() == QEvent.MouseButtonRelease and obj.rect().contains(event.pos()):
                self.clicked.emit()
                return True
            else:
                return False
    filter = Filter(widget)
    widget.installEventFilter(filter)
    return filter.clicked

clickable(self.lineedit).connect(self.test) #use this in class

def test(self):
    print("lineedit pressed")
    pass
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '21 at 09:18
0

Just use the Pushbutton clicked event. Change the backcolor of the Pushbutton into Transparent then remove the text of it. Lay it in front of the LineEdit then use the setfocus property of the LineEdit when the push button was clicked. That's the easiest way to get the clicked event and use it in LineEdit..

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 27 '22 at 13:08