网站单页是什么意思,高端网站建设熊掌号,网易企业邮箱服务器配置,网页设计实验报告html本文介绍如何获得QLineEdit的失去焦点事件和获得焦点的输入框也会触发失去焦点事件的问题#xff01; 目录
一、QLineEdit获得失去焦点事件
1.自定义类继承自QLineEdit
2.重写 focusOutEvent
3.使用
二、失去焦点事件问题
1.问题描述
2.问题解决
三、源码分享
lineed…本文介绍如何获得QLineEdit的失去焦点事件和获得焦点的输入框也会触发失去焦点事件的问题 目录
一、QLineEdit获得失去焦点事件
1.自定义类继承自QLineEdit
2.重写 focusOutEvent
3.使用
二、失去焦点事件问题
1.问题描述
2.问题解决
三、源码分享
lineeditfocus.h
lineeditfocus.cpp
widget.cpp 一、QLineEdit获得失去焦点事件
1.自定义类继承自QLineEdit
class LineEditFocus : public QLineEdit {Q_OBJECT
public:explicit LineEditFocus(QWidget *parent nullptr) { }~LineEditFocus() override { }
}
2.重写 focusOutEvent
protected:// 焦点离开void focusOutEvent(QFocusEvent *event) override { }// 获得焦点void focusInEvent(QFocusEvent *event) override { }
当获得焦点时focusInEvent方法会被触发当失去焦点时focusOutEvent方法会被触发
然后就可以在方法内部做一些我们的需求处理例如可以通过信号与槽通知主程序等
3.使用
直接使用我们自定义的类创建LineEditFocus对象即可
如果使用的是ui布局中的部件那么可以将部件提升为我们自定义的LineEditFocus即可 二、失去焦点事件问题
1.问题描述
如果有多个输入框部件且当前输入框部件失去焦点且另一个获得焦点的部件不是输入框时那么是没有问题的
如果当前输入框失去焦点且另一个获得焦点的部件也是输入框那么这样就会出现问题
会优先触发另一个输入框的失去焦点事件然后才会触发当前输入框的失去焦点事件最后再触发另一个输入框的获得焦点事件
案例
(1).继续在自定义类中添加信号 可以根据个人需求传输参数值例如可以将当前输入框的文本传送
signals:void signalLoseFocus(int index);
(2).然后在失去焦点方法中触发此信号
void LineEditFocus::focusOutEvent(QFocusEvent *event)
{static int index 1;emit signalLoseFocus(index);QLineEdit::focusOutEvent(event);
}
(3).最后在主窗体中使用即可ui部件提升即可
void Widget::init()
{LineEditFocus *le1 new LineEditFocus(this);LineEditFocus *le2 new LineEditFocus(this);LineEditFocus *le3 new LineEditFocus(this);QListLineEditFocus * list;list le1 le2 le3;foreach (LineEditFocus *lef, list) {lef-setFixedSize(350, 50);connect(lef, LineEditFocus::signalLoseFocus, this, Widget::onLeaveFocus);}QVBoxLayout *layout new QVBoxLayout(this);layout-addWidget(le1);layout-addWidget(le2);layout-addWidget(le3);this-setLayout(layout);
}
(4).在槽函数中将接收到的index通过messagebox提示出来
void Widget::onLeaveFocus(int index)
{QMessageBox::information(this, 提示, QString(输入框焦点离开%1).arg(index));
}
(5).运行效果 可以看出确实是有问题的 2.问题解决
具体是什么原因导致出现这样的问题我也没搞明白
但是办法总比困难多我们转换一下思路去解决他
(1).增加变量用于标志是否获取到了焦点
private:// 焦点获得标志bool m_focus;
(2).焦点获得获得焦点时m_focus赋值true
void LineEditFocus::focusInEvent(QFocusEvent *event)
{m_focus true; // 标志当前编辑框已经获得焦点QLineEdit::focusInEvent(event);
}
(3).失去焦点通过m_focus变量辅助配合判断
void LineEditFocus::focusOutEvent(QFocusEvent *event)
{if (m_focus event-lostFocus()) { // event-lostFocus(): type() FocusOutm_focus false; // 焦点失去emit signalLoseFocus(m_index);}QLineEdit::focusOutEvent(event);
}
通过上面的测试可知当从第一个输入框点击第二个输入框时优先触发第二个输入框的失去焦点事件此时第二个输入框是还没有获得焦点的即m_focus变量值为false信号就没法触发
紧接着第一个输入框的失去焦点事件触发因为先前已经获得了焦点即m_focus变量值为true所以第一个输入框的失去焦点事件可以正常发射信号
最后才会触发第二个输入框的获得焦点事件。 (3).运行测试 问题完美解决 三、源码分享
lineeditfocus.h
#ifndef LINEEDIT_FOCUS_H
#define LINEEDIT_FOCUS_H#include QLineEditclass LineEditFocus : public QLineEdit {Q_OBJECT
public:explicit LineEditFocus(QWidget *parent nullptr);~LineEditFocus() override;void SetIndex(int index);int GetIndex() const;signals:void signalLoseFocus(int index);void signalInFocus(int index);protected:// 焦点离开void focusOutEvent(QFocusEvent *event) override;// 获得焦点void focusInEvent(QFocusEvent *event) override;private:// 记录标志int m_index;// 焦点获得标志bool m_focus;
};#endif // LINEEDIT_FOCUS_Hlineeditfocus.cpp
#include lineeditfocus.h#include QFocusEventLineEditFocus::LineEditFocus(QWidget *parent) : QLineEdit(parent)
{m_index 0;m_focus false;
}LineEditFocus::~LineEditFocus()
{}void LineEditFocus::SetIndex(int index)
{m_index index;
}void LineEditFocus::focusOutEvent(QFocusEvent *event)
{if (m_focus event-lostFocus()) { // event-lostFocus() -- return type() FocusOut;m_focus false; // 焦点失去emit signalLoseFocus(m_index);}QLineEdit::focusOutEvent(event);
}void LineEditFocus::focusInEvent(QFocusEvent *event)
{m_focus true; // 焦点获得QLineEdit::focusInEvent(event);
}
widget.cpp
#include widget.h
#include ui_widget.h#include lineeditfocus.h#include QMessageBoxWidget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui-setupUi(this);init();
}Widget::~Widget()
{delete ui;
}void Widget::init()
{LineEditFocus *le1 new LineEditFocus(this);
// le1-setFixedSize(350, 50);
// le1-SetIndex(1);LineEditFocus *le2 new LineEditFocus(this);
// le2-setFixedSize(350, 50);
// le2-SetIndex(2);LineEditFocus *le3 new LineEditFocus(this);
// le3-setFixedSize(350, 50);
// le3-SetIndex(3);QListLineEditFocus * list;list le1 le2 le3;int index 1;foreach (LineEditFocus *lef, list) {lef-setFixedSize(350, 50);lef-SetIndex(index);connect(lef, LineEditFocus::signalLoseFocus, this, Widget::onLeaveFocus);}QVBoxLayout *layout new QVBoxLayout(this);layout-addWidget(le1);layout-addWidget(le2);layout-addWidget(le3);this-setLayout(layout);}void Widget::onLeaveFocus(int index)
{QMessageBox::information(this, 提示, QString(输入框焦点离开%1).arg(index));
} 完 文章转载自: http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.ktnmg.cn.gov.cn.ktnmg.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn