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(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(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

你可能感兴趣的:(qt按键焦点切换)