why my keyEvent activate after mousepress



  • My program is base on a stackedWidget and I create a base widget class,all the widget in the stackedWidget are inherite form the base class,I also write a eventFilter to response the key event,codes are below
    :
    void KeyManage::installFilter(QWidget* pwidget)
    {
    qDebug()<<pwidget->objectName();

    widget = pwidget;
     qDebug()<<widget->children().count();
    int a = 0;
    foreach(QObject *obj, widget->children())
    {
    
        QWidget *item = qobject_cast<QWidget*>(obj);
        qDebug()<<item->objectName();
        if(a==0)
        {
            currentControl =item;
            a=1;
        }
        item->installEventFilter(widget);
    }
    

    }

    bool KeyManage::setCurrentControl(QWidget* control)
    {
    currentControl = control;
    currentControl->setFocus();
    }

    bool KeyManage::eventFilter(QObject obj, QEvent event)
    {
    QKeyEvent
    keyEvent;
    if(event->type() == QEvent::KeyPress)
    {
    keyEvent = (QKeyEvent
    )event;
    if(Qt::Key_Up == keyEvent->key()||
    Qt::Key_Right == keyEvent->key()||
    Qt::Key_Left == keyEvent->key()||
    Qt::Key_Down == keyEvent->key())
    {
    operateForDirectionKey(keyEvent);
    return true;
    }
    else if (Qt::Key_Return == keyEvent->key())
    {
    operateForEnterKey();
    }
    }
    else
    {
    return false;
    }

    return false;
    

    }
    but when I remove a widget and add a widget to the stackedWidget , when I start the program ,the first widget in the StackedWisget are response right after I press a direction key,but From the second widget ,the direction key has no effect ,I must press the mouse on the Button(only the button,label or table are still no effect),then pressing the direction key will do right.Please give me some advise ,I will very appreciate it .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.