Bokambo
Bokambo

Reputation: 4480

How to get Click Event of QLineEdit in Qt?

How to get Click Event of QLineEdit in Qt ?

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

Upvotes: 22

Views: 36584

Answers (8)

H0lz3r-x64
H0lz3r-x64

Reputation: 31

In PyQT you can do something like this:

self.lineEdit_passwort.mousePressEvent = self.on_lineEdit_passwort_clicked

def on_lineEdit_passwort_clicked(self, *arg, **kwargs):
    print("lineEdit_passwort clicked!")

Works like a charm!

Upvotes: 2

TERMIBYTE
TERMIBYTE

Reputation: 1

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.. 😉

Upvotes: -1

All in One
All in One

Reputation: 1

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

Upvotes: 0

buck
buck

Reputation: 1532

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;
}

Upvotes: 29

Raiv
Raiv

Reputation: 5781

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

Upvotes: 1

Bowdzone
Bowdzone

Reputation: 3854

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.

Upvotes: 3

Raj
Raj

Reputation: 22956

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

Upvotes: 10

alfah
alfah

Reputation: 2085

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.

Upvotes: 2

Related Questions