Problem with keypress event?



  • I want to accept only enter key through keyboard but while i am implementing the following code i am getting some unexpected error
    @
    if (event->key () == Qt::Key_Enter)
    event->accept ();
    emit clicked();
    } else if (event->key () == Qt::Key_Space) {
    event->ignore ();
    }

        QPushButton::keyPressEvent (event);@
    

    While i am pressing the enter key of numlock it is working but while i am pressing the normal enter key it is not working and in both cases it is accepting the space key also.



  • The call of the keyPressEvent() function of QPushButton is always executed. I guess thats why you experience the described behaviour.

    Try to substitute the event->accept() call with the call of the QPushbutton::keyPressEvent() function and remove the last line.



  • [quote author="KA51O" date="1326114467"]The call of the keyPressEvent() function of QPushButton is always executed. I guess thats why you experience the described behaviour.

    Try to substitute the event->accept() call with the call of the QPushbutton::keyPressEvent() function and remove the last line.[/quote]

    you mean i should remove
    @QPushbutton::keyPressEvent() @



  • No, I mean you should only call the parents function if you want something to happen and if nothing should happen then don't call it. It's just a guess as I don't even know in which function the code you posted is used.

    @
    void YourClass::keyPressEvent(QKeyEvent* event)
    {
    if (event->key () == Qt::Key_Enter)
    {
    QPushButton::keyPressEvent (event);
    }
    }
    @

    This of course only works if the parents class default implementation is handling the event in the way you want it to.
    also you forgot the opening bracket for the first if clause.



  • Look for

    @
    Qt::Key_Enter
    Qt::Key_Return
    @

    These differ between return key on the keypad and on the alphanumeric keyboard, at least for some platforms.



  • [quote author="Volker" date="1326156195"]Look for

    @
    Qt::Key_Enter
    Qt::Key_Return
    @

    These differ between return key on the keypad and on the alphanumeric keyboard, at least for some platforms.[/quote]
    can we print during run time which key is pressed?



  • [quote author="KA51O" date="1326115947"]No, I mean you should only call the parents function if you want something to happen and if nothing should happen then don't call it. It's just a guess as I don't even know in which function the code you posted is used.

    @
    void YourClass::keyPressEvent(QKeyEvent* event)
    {
    if (event->key () == Qt::Key_Enter)
    {
    QPushButton::keyPressEvent (event);
    }
    }
    @

    This of course only works if the parents class default implementation is handling the event in the way you want it to.
    also you forgot the opening bracket for the first if clause.[/quote]
    I have tried this but only enter key of numeric keypad is working.



  • deepsigh

    @
    void YourClass::keyPressEvent(QKeyEvent* event)
    {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
    {
    QPushButton::keyPressEvent (event);
    }
    }
    @

    If getting to that is really too hard for you, then do yourself and everyone else on this planet a big favor: stop programming immediately!



  • [quote author="Volker" date="1326176791"]deepsigh

    @
    void YourClass::keyPressEvent(QKeyEvent* event)
    {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
    {
    QPushButton::keyPressEvent (event);
    }
    }
    @

    If getting to that is really too hard for you, then do yourself and everyone else on this planet a big favor: stop programming immediately![/quote]
    that is not the solution. Now the actual solution i got is to use
    @setDefault(true);@



  • trolling or for real ?



  • [quote author="KA51O" date="1326180354"]trolling or for real ?[/quote]

    Really that was the main problem.


Log in to reply
 

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