View Full Version : event or thread

2nd October 2008, 18:40

I have a keypad in dev/input/event0
I can read in the value like that :

main :

if ((fd = open( "dev/input/event0", O_RDONLY)) < 0) {
while (1)
rd = read(fd, ev, sizeof(struct input_event) * 64);

i want to use this value in an qt application but i can't put this in the main of my qtapp because the wile(1) stop the application and i can't do app->exec();

how can i do?
- use a thread : one to read my keypad and the other can do app->exec() to start my Qt application.
- or there is a mean to use the input event of linux like some event in a qt window but i don't know how i can that.

2nd October 2008, 19:47
see QSocketNotifier

2nd October 2008, 19:54
EDIT: sorry, I did not see Caduel's post above: he is right, please disregard what is said below

I'm afraid using a thread is the only option.

In order to use events you need to open QFile on a device and connect its readyRead() signal to a custom slot. But the problem is QFile does not emit signals:

Unlike other QIODevice implementations, such as QTcpSocket, QFile does not emit the aboutToClose(), bytesWritten(), or readyRead() signals. This implementation detail means that QFile is not suitable for reading and writing certain types of files, such as device files on Unix platforms.

And I do not see any other suitable subclasses of QIODevice that would allow opening devices.

2nd October 2008, 23:23
thanks for the response.

I have spent my afternoon in it and the socket looks an importante notion but i haven't yet understood how use it.

just to be sure : i can't connect a thread just like that ?
int main()
MyThread a;
MyThread b;
QObject::connect(a, SIGNAL(thekey()), b, SLOT(recep()));

tread.cpp: In function «int main()»:
tread.cpp:44: erreur: incomplete type «QObject» used in nested name specifier
tread.cpp:45: erreur: incomplete type «QObject» used in nested name specifier

that would be very easy.

i will try the socket thank you for your help

2nd October 2008, 23:58
Take a look at the attached example where I used Caduel's advice (QSocketNotifier). It works with /dev/input/mouse1 on my laptop and is specific to mouse events (size of event is 3 bytes). But it'll give you a starting point on how to use signals and slots without using threads.

I run it like this: sudo ./device /dev/input/mouse1

3rd October 2008, 15:39
it's magic, i think it is exactly what i want.
i will put it for qt3.

thank you very much

3rd October 2008, 18:43
it's ok that running I love the sockets now ;)