0

How can I define onmouseover and click event for a QLineEdit? I want to make 2 signals as onmouseover() and clicked() for QLineEdit

2 Answers 2

3

You can install an event filter on your QLineEdit.

Here's an example:

QLineEdit *line_edit = new QLineEdit(this);
ui->verticalLayout->addWidget(line_edit);
line_edit->installEventFilter(this);

And in your event filter function you can do something like this: (This is a function you override)

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        qDebug() << "CLICK";
    }
    if(event->type() == QEvent::MouseMove)
    {
        qDebug() << "MOUSE OVER";
    }
    return false;
}
1

I'm afraid, you'll have to inherit QLineEdit and override void mouseMoveEvent ( QMouseEvent * event ) and void mousePressEvent ( QMouseEvent * event ) (or void mouseReleaseEvent ( QMouseEvent * event ) if you wish). And don't forget to call setMouseTracking(true); to track mouse moves, when no mouse button is held.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.