赞
踩
1、使用
需要先添加:
ui->lineEdit->installEventFilter(this); //在窗体上为lineEdit1安装过滤器
头文件声明:
bool eventFilter(QObject *,QEvent *); //过滤器
- bool prompt::eventFilter(QObject *watched, QEvent *event)
- {
- if (watched == ui->lineEdit) //首先判断控件(这里指 lineEdit1)
- {
- if (event->type()==QEvent::FocusOut) // 这里指 lineEdit1 控件的失去焦点事件
- {
-
- }
- else if (event->type() == QEvent::KeyPress)//回车键
- {
- QKeyEvent *k = static_cast<QKeyEvent *>(event);
- if(k->key() == Qt::Key_Return)//主键盘回车键
- {
- qDebug() << "回车1";
- }
- else if(k->key() == Qt::Key_Enter)//右侧区域回车键
- {
- qDebug() << "回车2";
- }
-
- }
- }
- return QWidget::eventFilter(watched,event); // 最后将事件交给上层对话框
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。