我想把重点放在QLineEdit控件上。为了做到这一点,我在子类控件中重新实现了focusInEvent。问题是,它只适用于使用键盘进行聚焦。使用TAB。如果我用鼠标点击它或点击它(因为它是一个嵌入式应用程序),它似乎根本没有抓住这个事件。
我已经尝试过设置focusPolicy但没有运气。
.cpp文件:
#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文件:
#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;
};
关于我做错了什么的任何想法?
修改
因为在评论中提到我添加了ui标题的代码清单
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
答案 0 :(得分:1)
你基本上添加了一个
QLineEdit *lineEdit;
到您的fooLineEdit
,而QLineEdit
只是另一个setupUi
。
这发生在lineEdit = new QLineEdit(fooLineEdit);
:
fooLineEdit
我认为这足以让事情变得混乱(QLineEdit
的事件处理程序永远等待 inner class fooLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit fooLineEdit(QWidget *parent = 0);
signals:
void focused(bool hasFocus);
protected:
virtual void focusInEvent(QFocusEvent *e);
};
内很可能发生的事件
尝试在不使用ui的情况下实现QLineEdit子类。
标题
fooLineEdit::fooLineEdit(QWidget *parent) :
QLineEdit(parent)
{
this->setFocusPolicy(Qt::ClickFocus);
}
void fooLineEdit::focusInEvent(QFocusEvent *e)
{
qDebug() << e->reason();
QLineEdit::focusInEvent(e);
emit(focused(true));
}
来源:
span
看看问题是否已经消失。