Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt::Key_Down/Qt::Key_Up problem

    General and Desktop
    5
    12
    7924
    Loading More Posts
    • 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.
    • E
      elibziro last edited by

      I have class MyClass inherit from QTreeWidget,
      @
      class MyClass :public QTreeWidget
      {
      Q_OBJECT

      protected:
      virtual void keyPressEvent(QKeyEvent *e);
      };
      @
      My overridden keyPressEvent looks like this:
      @
      void MyClass ::keyPressEvent(QKeyEvent *event)
      {
      if ( event->key () == Qt::Key_Delete ) {...}
      if ( event->key () == Qt::Key_Down ) {...}
      if ( event->key () == Qt::Key_Up ) {...}
      ...
      }
      @
      This method don't work for Qt::Key_Up and Qt::Key_Down keys. For any others it's ok.
      I suppose that this is Qt BUG ????

      [Edit: Added @ tags -- mlong]

      1 Reply Last reply Reply Quote 0
      • A
        andre last edited by

        It might be that these keys are captured at an earlier stage than in the keyPressEvent handler of the widget. Try installing an event filter to find out. First, try the filter on the QTreeWidget itself. If that doesn't get you the keys, install it on the QApplication.

        1 Reply Last reply Reply Quote 0
        • B
          bhamuryen last edited by

          Hello,
          @ void MyClass ::keyPressEvent(QKeyEvent *event)
          {
          if ( event->key () == Qt::Key_Delete ) {...}
          if ( event->key () == Qt::Key_Down ) {...}
          if ( event->key () == Qt::Key_Up ) {...}
          ...
          }
          @
          what is the event?

          try this
          @ void MyClass ::keyPressEvent(QKeyEvent *e)
          {
          if ( e->key () == Qt::Key_Delete ) {...}
          if ( e->key () == Qt::Key_Down ) {...}
          if ( e->key () == Qt::Key_Up ) {...}
          ...
          }
          @

          or

          @ class MyClass :public QTreeWidget
          {
          Q_OBJECT

          protected:
              virtual void keyPressEvent(QKeyEvent *event);
          };
          

          @

          and be sure to include the #include <QKeyEvent>

          c++ Software Developer

          1 Reply Last reply Reply Quote 0
          • Q
            qxoz last edited by

            bhamuryen@ I think it doesn't matter.

            1 Reply Last reply Reply Quote 0
            • B
              bhamuryen last edited by

              Why? I think If you want use key events it's work fine. Why is not it work?

              c++ Software Developer

              1 Reply Last reply Reply Quote 0
              • S
                Sam last edited by

                [quote author="bhamuryen" date="1364331122"]
                what is the event?
                [/quote]

                Its just a variable/instance name it does not matter if you change from QKeyEvent *event to QKeyEvent *e.

                1 Reply Last reply Reply Quote 0
                • B
                  bhamuryen last edited by

                  i now its a variable.
                  this variable in header file
                  @virtual void keyPressEvent(QKeyEvent *e);@
                  and now in cpp file
                  @void MyClass ::keyPressEvent(QKeyEvent *event)@
                  two diffrent variable name. this is my main point.

                  [quote author="Sam" date="1364367957"]
                  [quote author="bhamuryen" date="1364331122"]
                  what is the event?
                  [/quote]

                  Its just a variable/instance name it does not matter if you change from QKeyEvent *event to QKeyEvent *e.[/quote]

                  c++ Software Developer

                  1 Reply Last reply Reply Quote 0
                  • E
                    elibziro last edited by

                    Thanks for all suggestions

                    I think that different variable name is not a problem in my case. I have the same name in header and cpp files in my code, but still doesn't work.
                    Now I will try with installing an event filter.

                    1 Reply Last reply Reply Quote 0
                    • S
                      Sam last edited by

                      @bhamuryen

                      The compiler only needs to know what kind of arguments the function requires. It's unimportant for the compiler how you call them. The function declaration only needs to specify the parameter types, order and the return type

                      you can also use
                      .h
                      @void keyPressEvent(QKeyEvent *);@

                      in you header file and it works as expected.

                      1 Reply Last reply Reply Quote 0
                      • B
                        bhamuryen last edited by

                        Do you have #include <QKeyEvent> in your project?

                        c++ Software Developer

                        1 Reply Last reply Reply Quote 0
                        • B
                          bhamuryen last edited by

                          Ok i tried
                          virtual void keyPressEvent(QKeyEvent *e);

                          void MyClass ::keyPressEvent(QKeyEvent *event)

                          this work fine ;) . But we want to use filter (like tihs if ( e->key () == Qt::Key_Up ) {...}) we need to add the @#include <QKeyEvent>@

                          [quote author="Sam" date="1364369377"]@bhamuryen

                          The compiler only needs to know what kind of arguments the function requires. It's unimportant for the compiler how you call them. The function declaration only needs to specify the parameter types, order and the return type

                          you can also use
                          .h
                          @void keyPressEvent(QKeyEvent *);@

                          in you header file and it works as expected.[/quote]

                          c++ Software Developer

                          1 Reply Last reply Reply Quote 0
                          • S
                            Sam last edited by

                            @bhamuryen

                            Yes ofcourse you need to add QKeyEvent header to your project inorder to access the member function of the class.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post