PDA

View Full Version : if(event->type() == QEvent::KeyRelease) gives error



schooner
20th March 2014, 17:18
Hi

This is driving me nuts, so hopefully someone has the answer.

I am re-implementing QApplication to filter all mouse and keyboard events to a widget which is embedded into a tk application.
tk does not pass on focus properly and the tk app needs some crucial keystrokes forwarded, so I have to check everything
including checking which child widget is the receiver and forcing focus in some instances.

I just finished dealing with mouse clicks and went to add keystrokes


bool FilterApplication::notify( QObject *receiver, QEvent *event )
{
if(event->type() == QEvent::MouseButtonPress)
qDebug() << "Button Pressed";
if(event->type() == QEvent::KeyRelease)
qDebug() << "Key Released";

.......

}

The test for mouse events is fine, but when I try to do the same for key press / release events I get

FilterApplication.cpp: In member function ‘virtual bool FilterApplication::notify(QObject*, QEvent*)’:
FilterApplication.cpp:33:29: error: expected unqualified-id before numeric constant
FilterApplication.cpp:33:29: error: expected ‘)’ before numeric constant
make: *** [FilterApplication.o] Error 1

I can cast the event eg.



QKeyEvent *K = (QKeyEvent*)event;
if (K->key() == Qt::Key_Escape)
qDebug() << "Escape Pressed";

That works fine, except that I get 3 key codes instead of 1 for release and I am casting and testing stuff that isn't key events

Hopefully something simple, but my head is scrambled trying to find it.

Qt 4.8 on Linux

regards

anda_skoa
20th March 2014, 17:32
Maybe a typo or something similar?

A quick test:


#include <QApplication>
#include <QDebug>
#include <QKeyEvent>
#include <QLineEdit>

class Filter : public QObject
{
public:
bool eventFilter(QObject *watched, QEvent *event)
{
switch (event->type()) {
case QEvent::KeyPress:
qDebug() << "Key Press Event for" << watched << ":" << static_cast<QKeyEvent*>(event)->text();
break;
case QEvent::KeyRelease:
qDebug() << "Key Release Event for" << watched << ":" << static_cast<QKeyEvent*>(event)->text();
break;
default:
break;
}

return false; // do not filter
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

app.installEventFilter(new Filter);

QLineEdit lineEdit;
lineEdit.show();

return app.exec();
}


Cheers,
_

schooner
20th March 2014, 17:51
Hi


Maybe a typo or something similar?

You would think so wouldn't you?

This gives the same error for the KeyEvent tests but not the MouseEvent test


switch (event->type())
{
case QEvent::KeyPress:
qDebug() << "Key Press Event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
break;
case QEvent::KeyRelease:
qDebug() << "Key Release Event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
break;
case QEvent::MouseButtonDblClick:
qDebug() << "Mouse button doubleclick event for" << receiver << ":" << static_cast<QKeyEvent*>(event)->text();
break;
default:
break;
}

Error

-o FilterApplication.o FilterApplication.cpp
FilterApplication.cpp: In member function ‘virtual bool FilterApplication::notify(QObject*, QEvent*)’:
FilterApplication.cpp:52:22: error: expected unqualified-id before numeric constant
FilterApplication.cpp:52:22: error: expected ‘:’ before numeric constant
FilterApplication.cpp:52:30: error: expected ‘;’ before ‘:’ token
FilterApplication.cpp:55:22: error: expected unqualified-id before numeric constant
FilterApplication.cpp:55:22: error: expected ‘:’ before numeric constant
FilterApplication.cpp:55:32: error: expected ‘;’ before ‘:’ token
make: *** [FilterApplication.o] Error 1


I have now found the cause of the error

In a global header, the Xlib headers required for X forwarding through XSendEvent are included
#include <X11/Xlib.h>
#include <X11/keysym.h>

I suspect there is a #define clash between keysym.h and the Qt KeyEvent headers
Have not tracked it down yet, but commenting out the includes allows compilation, so I am off again

regards