Foufoutos
Foufoutos

Reputation: 17

Qt focusInEvent() grabs focus only for keyboard

I want to grab the focus on a QLineEdit control. In order to do that, I reimplemented the focusInEvent in my subclassed control. The problem is, that it works only for focusing with keyboard, aka. using the TAB. If I click it with the mouse or tap it (since it's an embedded app) it doesn't seem to grab the event at all.

I already tried setting the focusPolicy but without any luck.

.cpp file:

#include "foolineedit.h"
#include "ui_foolineedit.h"
#include <QDebug>
#include <QFocusEvent>

fooLineEdit::fooLineEdit(QWidget *parent) :
    QLineEdit(parent),
    ui(new Ui::fooLineEdit)
{
    ui->setupUi(this);
    this->setFocusPolicy(Qt::ClickFocus);
}

fooLineEdit::~fooLineEdit()
{
    delete ui;
}

void fooLineEdit::focusInEvent(QFocusEvent *e)
{
    qDebug() << e->reason();
    QLineEdit::focusInEvent(e);
    emit(focused(true));
}

.h file:

#include <QWidget>
#include <QLineEdit>

namespace Ui {
class fooLineEdit;
}

class fooLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    explicit fooLineEdit(QWidget *parent = 0);
    ~fooLineEdit();
signals:
    void focused(bool hasFocus);
protected:
    virtual void focusInEvent(QFocusEvent *e);
private:
    Ui::fooLineEdit *ui;
};

Any ideas about what am I doing wrong?


EDIT:

Because it was mentioned in comments I add the code listing for the ui header

ui_foolineedit.h

class Ui_fooLineEdit
{
public:
    QLineEdit *lineEdit;

    void setupUi(QWidget *fooLineEdit)
    {
        if (fooLineEdit->objectName().isEmpty())
            fooLineEdit->setObjectName(QStringLiteral("fooLineEdit"));
        fooLineEdit->resize(151, 21);
        lineEdit = new QLineEdit(fooLineEdit);
        lineEdit->setObjectName(QStringLiteral("lineEdit"));
        lineEdit->setGeometry(QRect(0, 0, 151, 21));
        lineEdit->setFocusPolicy(Qt::ClickFocus);

        retranslateUi(fooLineEdit);

        QMetaObject::connectSlotsByName(fooLineEdit);
    } // setupUi

    void retranslateUi(QWidget *fooLineEdit)
    {
        fooLineEdit->setWindowTitle(QApplication::translate("fooLineEdit", "Form", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class fooLineEdit: public Ui_fooLineEdit {};
} // namespace Ui

Upvotes: 0

Views: 741

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

You basically add a

QLineEdit *lineEdit;

to your fooLineEdit, which in turn is just another QLineEdit.

This happens in setupUi:

lineEdit = new QLineEdit(fooLineEdit);

and I think this is enough to mess things up (the event handler of fooLineEdit waits forever for an event which is very likely to occur inside the inner QLineEdit).

Try implementing a QLineEdit subclass without using an ui.

Header:

class fooLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    explicit fooLineEdit(QWidget *parent = 0);
signals:
    void focused(bool hasFocus);
protected:
    virtual void focusInEvent(QFocusEvent *e);
};

Source:

fooLineEdit::fooLineEdit(QWidget *parent) :
    QLineEdit(parent)
{
    this->setFocusPolicy(Qt::ClickFocus);
}
void fooLineEdit::focusInEvent(QFocusEvent *e)
{
    qDebug() << e->reason();
    QLineEdit::focusInEvent(e);
    emit(focused(true));
}

And see if the issue is gone.

Upvotes: 1

Related Questions