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