Qt自由切换焦点

      这次的项目中有个界面大概需要实现这样的一个功能:当我按键盘Tab键时,焦点在pushButton、pushButton_5、pushButton_9之间切换,当焦点停在pushButton、pushButton_5、pushButton_9之间某个按键时,此时按键盘的向下键焦点在其下方的按键来回切换,例如现在焦点在pushButton_5,我按向下键时,焦点会在pushButton_6、pushButton_7、pushButton_8之间来回切换。有点绕啊!

    可是我现在写得代码能够实现:当焦点在pushButton时,按向下键时,能够在pushButton_2、pushButton_3、pushButton_4之间切换。按Tab键,能实现焦点在pushButton、pushButton_5、pushButton_9之间来回切换,但是当焦点在pushButton_5时,按向下键焦点会在所有的按键当中来回切换,当回到pushButton时,又只能在pushButton_2、pushButton_3、pushButton_4之间切换了。

Qt自由切换焦点_第1张图片

      真是奇了怪了,能在其中的几个按钮之间实现功能,按照同样的方式居然不能达到预期的效果,真是整了一整天没搞出来,有点小郁闷啊

     下面的代码是我为了实现所需功能的代码,希望那位大神帮我看看,找出问题所在。

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
     bool eventFilter(QObject *,QEvent *);    //声明一个事件过滤槽函数,用于事件的


private:
    Ui::Widget *ui;


};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include 
#include 

#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->pushButton->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_2->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_3->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_4->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_5->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_6->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_7->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_8->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_9->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_10->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_11->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器
    ui->pushButton_12->installEventFilter(this);  //在窗体上为lineEdit1安装过滤器



}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
        if(
                watched==ui->pushButton||
                watched==ui->pushButton_2||
                watched==ui->pushButton_3||
                watched==ui->pushButton_4||
                watched==ui->pushButton_5||
                watched==ui->pushButton_6||
                watched==ui->pushButton_7||
                watched==ui->pushButton_8||
                watched==ui->pushButton_9||
                watched==ui->pushButton_10||
                watched==ui->pushButton_11||
                watched==ui->pushButton_12
           )
        {

               if(event->type()==QEvent::KeyPress)
                  {
                      QKeyEvent *key_event = static_cast < QKeyEvent *>(event); //将事件转化为键盘事件

                      if(key_event->key() == Qt::Key_Tab)//下键
                      {
                          bool  pushButton =ui->pushButton->hasFocus();
                          bool  pushButton_5 =ui->pushButton_5->hasFocus();
                          bool  pushButton_9 =ui->pushButton_9->hasFocus();
                          if( pushButton)//焦点在pushButton处
                          {
                            focusNextChild();//焦点移动
                            ui->pushButton_5->setFocus();//移动到apushButton_2
                          }
                          else if(pushButton_5)//焦点在apushButton_5处
                          {
                            focusNextChild();//焦点移动
                            ui->pushButton_9->setFocus();//移动到pushButton_3
                          }
                          else if(pushButton_9)//焦点在pushButton_9处
                          {
                            focusNextChild();//焦点移动
                            ui->pushButton->setFocus();//移动到pushButton_4
                          }
                          else
                          {
                              focusNextChild();//焦点移动
                          }
                          return true;
                      }

                      if(key_event->key() == Qt::Key_Down)//下键
                      {

                             bool  pushButton =ui->pushButton->hasFocus();
                             bool  pushButton_2 =ui->pushButton_2->hasFocus();
                             bool  pushButton_3 =ui->pushButton_3->hasFocus();
                             bool  pushButton_4 =ui->pushButton_4->hasFocus();//定义4个获得焦点的状态

                             if(pushButton)//焦点在pushButton处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_2->setFocus();//移动到apushButton_2
                             }
                             else if(pushButton_2)//焦点在apushButton_2处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_3->setFocus();//移动到pushButton_3
                             }
                             else if(pushButton_3)//焦点在pushButton_3处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_4->setFocus();//移动到pushButton_4
                             }
                             else if(pushButton_4)//焦点在pushButton_3处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_2->setFocus();//移动到pushButton_2
                             }
                             else
                             {
                                 focusNextChild();//焦点移动
                             }
                             return true;
                       }
                      if(key_event->key() == Qt::Key_Down)//下键
                      {

                             bool  pushButton_5 =ui->pushButton_5->hasFocus();
                             bool  pushButton_6 =ui->pushButton_6->hasFocus();
                             bool  pushButton_7 =ui->pushButton_7->hasFocus();
                             bool  pushButton_8 =ui->pushButton_8->hasFocus();//定义4个获得焦点的状态

                             if(pushButton_5)//焦点在pushButton处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_6->setFocus();//移动到apushButton_2
                             }
                             else if(pushButton_6)//焦点在apushButton_6处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_7->setFocus();//移动到pushButton_3
                             }
                             else if(pushButton_7)//焦点在pushButton_7处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_8->setFocus();//移动到pushButton_4
                             }
                             else if(pushButton_8)//焦点在pushButton_8处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_7->setFocus();//移动到pushButton_2
                             }
                             else
                             {
                                 focusNextChild();//焦点移动
                             }
                             return true;
                       }
                      if(key_event->key() == Qt::Key_Left)//下键
                      {

                             bool  pushButton_9 =ui->pushButton_9->hasFocus();
                             bool  pushButton_10 =ui->pushButton_10->hasFocus();
                             bool  pushButton_11 =ui->pushButton_11->hasFocus();
                             bool  pushButton_12 =ui->pushButton_12->hasFocus();//定义4个获得焦点的状态

                             if(pushButton_9)//焦点在pushButton处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_10->setFocus();//移动到apushButton_2
                             }
                             else if(pushButton_10)//焦点在apushButton_10处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_11->setFocus();//移动到pushButton_3
                             }
                             else if(pushButton_11)//焦点在pushButton_11处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_12->setFocus();//移动到pushButton_4
                             }
                             else if(pushButton_12)//焦点在pushButton_12处
                             {
                               focusNextChild();//焦点移动
                               ui->pushButton_10->setFocus();//移动到pushButton_2
                             }
                             else
                             {
                                 focusNextChild();//焦点移动
                             }
                             return true;
                       }
                  }

        }


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





    上面就是整个代码了,其中main.cpp没有改动,所以就没贴上来。

     代码编译完全没有错误,但是不能实现预期的功能,感觉是逻辑上的问题,但是检查几遍都没检查出啥情况,Qt界的大神们给我看看吧,感激不尽




你可能感兴趣的:(Qt)