Qt 解决用QLineEdit实现的输入框开始没有光标在闪烁的问题
在项目中遇到new出来QLineEdit在开始时,没有光标在闪烁,此时需要重写QlineEdit
代码如下:
.h文件
class CLineEidt : public QLineEdit
{
Q_OBJECT
public:
CLineEidt(QWidget *parent = NULL);
~CLineEidt();
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
virtual void showEvent(QShowEvent *event);
private:
signals :
void focusOut(CLineEidt *);
void focusIn(CLineEidt *);
};
.cpp文件
CLineEidt::CLineEidt(QWidget *parent /*= NULL*/) :QLineEdit(parent)
{
}
CLineEidt::~CLineEidt()
{
}
void CLineEidt::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
emit focusIn(this);
}
void CLineEidt::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
emit focusOut(this);
}
void CLineEidt::showEvent(QShowEvent *event)
{
setEnabled(true);
setFocus();
QLineEdit::showEvent(event);
}
调用如下:
CLineEidt *m_filterInputLineEdit = new CLineEidt();
m_filterInputLineEdit->setObjectName("filterInputLineEdit");
m_filterInputLineEdit->setFixedHeight(40);
m_filterInputLineEdit->setMaxLength(60);
m_filterInputLineEdit->setStyleSheet("QLineEdit#filterInputLineEdit{border: 1px solid rgb(224,229,237);
}");
//关联槽函数,处理失去焦点与获得焦点的响应函数
connect(m_filterInputLineEdit, SIGNAL(focusOut(CLineEidt*)), this, SLOT(onFoucusOut(CLineEidt*)));
connect(m_filterInputLineEdit, SIGNAL(focusIn(CLineEidt*)), this, SLOT(onFoucusIn(CLineEidt*)));
【Qt 解决用QLineEdit实现的输入框开始没有光标在闪烁的问题】
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- Docker应用:容器间通信与Mariadb数据库主从复制
- JS中的各种宽高度定义及其应用
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- 涉毒患者(新诗)
- 参保人员因患病来不及到指定的医疗机构就医,能否报销医疗费用()
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决