Chinaunix首页 | 论坛 | 博客
  • 博客访问: 127939
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 55
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-16 10:13
个人简介

努力成为大神~

文章存档

2018年(4)

2016年(40)

我的朋友

分类: 嵌入式

2018-01-18 21:58:40

在使用事件过滤器对用户的按键行为进行判断时,发现当按下上下左右方向键后,焦点并没有按照代码中实现的逻辑进行移动。后经过调试发现,在事件过滤器中,对于每一个事件的处理后均需要返回一个返回值,不然则会出现上述的问题。

点击(此处)折叠或打开

  1. bool qwidget::eventFilter(QObject *watched, QEvent *event)
  2. {
  3.     if(watched == x_qwidget || watched == y_qwidget || ........) //大量的需要进行监控的组件,记得installEventFilter
  4.     {
  5.         if(event->type() == QEvent::KeyPress)
  6.         {
  7.             QKeyEvent *key_event = static_cast<QKeyEvent *>(event);

  8.             bool x_qwidget_focus = x_qwidget->hasFocus(); //焦点获取判断
  9.             bool y_qwidget_focus = y_qwidget->hasFocus();
  10.             ............
  11.                 
  12.             
  13.             if(key_event->key() == Qt::Key_Up)
  14.             {
  15.                 if(x_qwidget_focus)
  16.                 {
  17.                     focusNextChild(); //焦点移动
  18.                     ........ //某一个新组件获得焦点
  19.                 }
  20.                 else if(y_qwidget_focus)
  21.                 {
  22.                     focusNextChild();
  23.                     .........
  24.                 }
  25.                 ........
  26.                 return true; //完成处理后返回true
  27.             }
  28.             else if(key_event->key() == Qt::Key_Down)
  29.             {
  30.                  if(x_qwidget_focus)
  31.                 {
  32.                     focusNextChild(); //焦点移动
  33.                     ........ //某一个新组件获得焦点
  34.                 }
  35.                 else if(y_qwidget_focus)
  36.                 {
  37.                     focusNextChild();
  38.                     .........
  39.                 }
  40.                 ........
  41.                 return true;
  42.             }
  43.             else if(key_event->key() == Qt::Key_Left)
  44.             {
  45.                 if(x_qwidget_focus)
  46.                 {
  47.                     focusNextChild(); //焦点移动
  48.                     ........ //某一个新组件获得焦点
  49.                 }
  50.                 else if(y_qwidget_focus)
  51.                 {
  52.                     focusNextChild();
  53.                     .........
  54.                 }
  55.                 ........
  56.                 return true;
  57.             }
  58.             else if(key_event->key() == Qt::Key_Right)
  59.             {
  60.                 if(x_qwidget_focus)
  61.                 {
  62.                     focusNextChild(); //焦点移动
  63.                     ........ //某一个新组件获得焦点
  64.                 }
  65.                 else if(y_qwidget_focus)
  66.                 {
  67.                     focusNextChild();
  68.                     .........
  69.                 }
  70.                 ........
  71.                 return true;
  72.             }
  73.             else if(key_event->key() == Qt::Key_Return)
  74.             {
  75.                 if(x_qwidget_focus)
  76.                 {
  77.                     focusNextChild(); //焦点移动
  78.                     ........ //某一个新组件获得焦点
  79.                 }
  80.                 else if(y_qwidget_focus)
  81.                 {
  82.                     focusNextChild();
  83.                     .........
  84.                 }
  85.                 ........
  86.                 return true;
  87.             }
  88.             else
  89.             {
  90.                 return false; //不在按键事件中返回false
  91.             }
  92.          }
  93.          else
  94.         {
  95.              return false;//不为按键事件返回false
  96.         }
  97.     }
  98.     return false; //不为监控组件中成员返回false
  99. }

这样实现后经验证不会出现焦点移动错误的问题
补充:
如果在事件过滤器中不进行正确值的返回,编译运行以及移植到ARM平台运行时会出现组件显示缺失的问题。
阅读(2179) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~