Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Can't catch Enter(Return) Keypressed event
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roseicollis
    wrote on 21 Jan 2015, 15:30 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 21 Jan 2015, 15:42 last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roseicollis
        wrote on 21 Jan 2015, 15:57 last edited by
        #3

        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.

        1 Reply Last reply
        0

        1/3

        21 Jan 2015, 15:30

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved