qt按键焦点切换

实现内容

对于4个控件,当按下左右按键时焦点可以左右切换,如下图:


关键代码

1.实现焦点切换功能

使用事件过滤器来对事件进行分类处理这样就可以在a1至a4等控件上实现,按下右键后即把焦点转到右边控件上的功能。

先在aa.h中加入

public slots:

booleventFilter(QObject *,QEvent *);

在aa.cpp中定义函数

bool aa::eventFilter(QObject *watched, QEvent*event)

{

   if(watched == ui->a1 || watched == ui->a2 || watched == ui->a3|| watched == ui->a4)//确认可控的控件

    {

       if(event->type() == QEvent::KeyPress)//如果检测到按键

         {

              bool  f1 =ui->a1->hasFocus();

              bool  f2 =ui->a2->hasFocus();

              bool  f3 =ui->a3->hasFocus();

              bool  f4 =ui->a4->hasFocus();定义4个获得焦点的状态

           QKeyEvent *key_event = static_cast<QKeyEvent *>(event); //将事件转化为键盘事件

             if(key_event->key() == Qt::Key_Right)//右键

             {

                 if(f1)//焦点在a1

                {

                   focusNextChild();//焦点移动

                ui->a2->setFocus();//移动到a2

                }

                 if(f2)

               {

                   focusNextChild();

                ui->a3->setFocus();

                }

                 if(f3)

                {

                   focusNextChild();

                ui->a4->setFocus();

                }

             }

             if(key_event->key() == Qt::Key_Left)

             {

                 if(f4)

                {

                   focusNextChild();

                ui->a3->setFocus();

                }

                 if(f3)

                {

                  focusNextChild();

                ui->a2->setFocus();

                }

                 if(f2)

                {

                   focusNextChild();

                ui->a1->setFocus();

                }

             }

       }

}

return QWidget::eventFilter(watched,event);     //最后将事件交给上层对话框

}

左键功能定义稍加修改即可

 

2.实现动态焦点背景色变化功能

if (watched==ui->a1)         //

     {

         if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件(这里指获得焦点事件)

         {

             QPalette p=QPalette();

             p.setColor(QPalette::Button,Qt::yellow);

             ui->a1->setPalette(p);

         }

另一种实现方法

另外一般的焦点切换还可以使用setCurrentItem函数实现,下面是在网上看到的代码:

bool MainForm::eventFilter(QObject *obj,QEvent *event)

{

         if(obj==ComboBox_Keyboard){       

                   if(event->type()== QEvent::KeyPress)

                   {

                            QKeyEvent*keyEvent = static_cast<QKeyEvent*>(event);

                            if(keyEvent->key()== 4100)//ENTER

                                     on_ComboBox_Keyboard(ComboBox_Keyboard->currentItem());

                            if(keyEvent->key()== 4117){//DOWN

                                     if(ComboBox_Keyboard->currentItem()==ComboBox_Keyboard->count()-1)

                                               ComboBox_Keyboard->setCurrentItem(0);

                                     else

                                               ComboBox_Keyboard->setCurrentItem(ComboBox_Keyboard->currentItem()+1);}

                            if(keyEvent->key()== 4115){//UP

                                     if(ComboBox_Keyboard->currentItem()==0)

                                               ComboBox_Keyboard->setCurrentItem(ComboBox_Keyboard->count()-1);

                                     else

                                               ComboBox_Keyboard->setCurrentItem(ComboBox_Keyboard->currentItem()-1);}

                            returnTRUE;            

                   }else{

                            returnFALSE;

                   }

         }

…………

}

 

参考资料:

http://zoreraul.blog.163.com/blog/static/725739282010828104621410/

http://zhidao.baidu.com/link?url=4DCT55CnwXzT1nC28f0oPb36rrlK7bp0eohV61gtlZY0IvzLucMRqMvIjNHq4Oc5_horjUhyDkeSy0A1VYW7hK

  • 0
    点赞
  • 0
    评论
  • 8
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值