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. Problem with keypress event?
Forum Updated to NodeBB v4.3 + New Features

Problem with keypress event?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 12.4k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    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.

    Pratik Agrawal

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #3

        [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() @

        Pratik Agrawal

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pratik041
              wrote on last edited by
              #6

              [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?

              Pratik Agrawal

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pratik041
                wrote on last edited by
                #7

                [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.

                Pratik Agrawal

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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!

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pratik041
                    wrote on last edited by
                    #9

                    [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);@

                    Pratik Agrawal

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KA51O
                      wrote on last edited by
                      #10

                      trolling or for real ?

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pratik041
                        wrote on last edited by
                        #11

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

                        Really that was the main problem.

                        Pratik Agrawal

                        1 Reply Last reply
                        0

                        • Login

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