PDA

View Full Version : eventFilter in Console App



KaptainKarl
18th December 2008, 17:29
QT Version: 4.4.1
OS: RHEL 5

I am attempting to create a console application that uses QLocalServer/QLocalSocket for interprocess communication.

In the program, I stay in the event loop waiting for connections.

If the user presses a key, I want to exit the program.

I have remplemented the eventFilter method and added a installEventFilter(this) call to the constructor of the SNOOPER class:


class SNOOPER: public QObject
{
Q_OBJECT

public:
SNOOPER(QString sIP); // Constructor
~SNOOPER(); // Destructor
...

bool eventFilter( QObject *o, QEvent *e );

public slots:
void connectClient();
void readParts();
};

bool SNOOPER::eventFilter( QObject *o, QEvent *e )
{
bool bRval = false;

if (e->type() == QEvent::KeyPress)
{
qDebug("KeyPress event detected.");
bRval = true;
exit(0);
}
else
{
qDebug("Non-KeyPress event detected.");
}
return bRval;
}I received the following output when I run the program, but I do not get any key events:


Non-KeyPress event detected.
Non-KeyPress event detected.Please let me know what I am missing.

Thanks!
Karl

wysota
19th December 2008, 08:40
KeyPressEvents are sent only to widgets therefore console applications won't receive them.

KaptainKarl
19th December 2008, 13:35
Is there a good way to trap <control>-C so that I can at least shutdown the QLocalServer if they exit that way?

Currently, it works like this:
1. Start the program.
2. Hit Control-C to stop it.
3. Start it again, and I get a socket busy error.
4. Start it again and it works.

If I could somehow skip step 3, I'd be alright.

Karl

wysota
20th December 2008, 00:27
Use signal() or sigaction() depending on the system you are using.