Qt::Key_Down/Qt::Key_Up problem



  • 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]



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



  • 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>



  • bhamuryen@ I think it doesn't matter.



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



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



  • 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]



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



  • @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.



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



  • 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]



  • @bhamuryen

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


Log in to reply
 

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