[Solved] Can't catch Enter(Return) Keypressed event



  • Hi again!

    I'm making a GUI app with a wizard-wizardpages structure. In the first page(WPInicial) I have 3 big buttons so you can switch between them with tab, and then go to the next wizard page using 'P'. The thing is that I don't want to use 'P'. I want to use the Enter key to go to the next page.

    I've tried to put this 3 functions but if I press Enter and debug it jsut enters in :event() but never print 2 or 3 (never is true the if condition of Qt::Key_Enter or Key_Return). Any idea of why and how can I do what I want?

    P.D: 13 , Enter, unknow and return debug texts from keyPressEvent() are never printed as well.

    And the 3 buttons focus are defined as
    @
    _b1->setFocusPolicy(Qt::TabFocus);
    _b2->setFocusPolicy(Qt::TabFocus);
    _b3->setFocusPolicy(Qt::TabFocus);
    @

    @

    bool WPInicial::event(QEvent* qe)
    {
    if (qe->type() == QEvent::KeyPress)
    {
    QKeyEvent *ke = (QKeyEvent *) qe;
    if (ke->key() == Qt::Key_Tab)
    {
    qDebug() << " 1 ";
    return true;
    }
    if (ke->key() == Qt::Key_Enter)
    {
    qDebug() << " 2 ";
    return true;
    }
    if (ke->key() == Qt::Key_Return)
    {
    qDebug() << " 3 ";
    return true;
    }
    if (ke->key() == Qt::Key_Space)
    {
    qDebug() << " 4";
    return true;
    }
    return true;
    }
    return QWidget::event(qe);
    }

    bool WPInicial::eventFilter(QObject *object, QEvent *event)
    {
    if (object == _b1 && event->type() == QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Return)
    {
    // Special tab handling
    qDebug("Enter Key Pressed...");
    return true;
    }
    else
    {
    return WPInicial::eventFilter(object, event);
    }
    }
    else
    {
    return WPInicial::eventFilter(object, event);
    }
    }

    void WPInicial::keyPressEvent(QKeyEvent *event) // definition
    {

    event->accept();
    
    QWidget::keyPressEvent(event);
    switch(event->key())                   
    {
    case 13: qDebug() << " __13__"; break;
    case Qt::Key_Left: break;
    case Qt::Key_G: qDebug() << " __ G __"; break;
    case Qt::Key_Enter: qDebug() << " ___ENTER___"; break;
    case Qt::Key_Return: qDebug() << " __return ___"; break;
    case Qt::Key_P:wizard()->next(); break;
    case Qt::Key_U:
    case Qt::Key_Up:
    case Qt::Key_Down: break;
    case Qt::Key_unknown: qDebug() << " _unknown_"; break;
        break;
    }
    

    }
    @

    Thank you!


  • Moderators

    There are some ways to achieve what you want.

    you could install an event filter in the QApplication to receive the key event before any widget will receive it

    set shortcuts for your widget

    hard to tell whats wrong in your code since there may be other thing which influence your event handlers and event filter.



  • Hi raven-worx,

    thanks for your answer. I've see that too: http://qt-project.org/wiki/How_to_catch_enter_key and finally it works. I needed to install the filter in every widget I need.


Log in to reply
 

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