Qt::Key_Down/Qt::Key_Up problem
-
wrote on 26 Mar 2013, 14:01 last edited by
I have class MyClass inherit from QTreeWidget,
@
class MyClass :public QTreeWidget
{
Q_OBJECTprotected:
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]
-
wrote on 26 Mar 2013, 16:02 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.
-
wrote on 26 Mar 2013, 20:52 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_OBJECTprotected: virtual void keyPressEvent(QKeyEvent *event); };
@
and be sure to include the #include <QKeyEvent>
-
wrote on 27 Mar 2013, 05:14 last edited by
bhamuryen@ I think it doesn't matter.
-
wrote on 27 Mar 2013, 06:30 last edited by
Why? I think If you want use key events it's work fine. Why is not it work?
-
wrote on 27 Mar 2013, 07:05 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.
-
wrote on 27 Mar 2013, 07:23 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]
-
wrote on 27 Mar 2013, 07:28 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. -
wrote on 27 Mar 2013, 07:29 last edited by
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.
-
wrote on 27 Mar 2013, 07:44 last edited by
Do you have #include <QKeyEvent> in your project?
-
wrote on 27 Mar 2013, 07:49 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]
-
wrote on 27 Mar 2013, 07:54 last edited by
Yes ofcourse you need to add QKeyEvent header to your project inorder to access the member function of the class.
1/12