PDA

View Full Version : Producing Keypressed Event



anafor2004
24th April 2008, 10:33
Hi All
I am writing a Qt program, I want to change color of my menu by pressing a key on keyboard. I wonder that , may i make the same effect by code not pressing on keyboard?

Infact how my i make call keypress event without pressing any key?

merlvingian
24th April 2008, 11:32
Look into QEvent(), QCoreApplication::sendEvent() , and QCoreApplication::postEvent()

marcel
24th April 2008, 11:34
QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, Qt::Key_0 ,Qt::NoModifier);
qApp->postEvent(objectThatHandlesKeypresses, ev);


But if you are only doing this to programatically change the color of a menu item, then it is wrong. You could directly call the function that does that.

anafor2004
29th April 2008, 13:27
I tried to do this but it didnt work? I wrote it to main function.

jpn
29th April 2008, 13:30
I tried to do this but it didnt work? I wrote it to main function.
What did you write? What do you expect it to do?

anafor2004
29th April 2008, 13:47
QApplication a(argc, argv);
gkdpr w;
QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, Qt::Key_0 ,Qt::NoModifier);
QApplication::sendEvent(w,ev);
w.setGeometry(0,0,800,480);
//w.setWindowFlags(Qt::FramelessWindowHint);
w.show();
a.installTranslator(&w.translator);
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();

I wrote this code.I want to send keypress event to "w". Is it possible?

anafor2004
29th April 2008, 14:12
main.cpp:16: error: no matching function for call to ‘QCoreApplication::sendEvent(gkdpr&, QKeyEvent*&)’
/usr/include/QtCore/qcoreapplication.h:201: note: candidates are: static bool QCoreApplication::sendEvent(QObject*, QEvent*)
make[1]: *** [moc/main.o] Error 1
make: *** [x86] Error 2
make[1]: Leaving directory `/home/can/finalize/workspace/gkdpr'

wysota
29th April 2008, 16:10
Why don't you just trigger() the QAction object asociated with the menu entry?

anafor2004
30th April 2008, 07:06
Dear wysota,
I want to read a file and according to information in that file, I want to make this keypress effect by reading a file. I don't have any keyboard on my device. The other way i will use a serial port. According to information of this port i want to simulate this keypress events.

May I do this with QAction?

wysota
30th April 2008, 08:05
What do you need those events for? What is the goal of your piece of code?

anafor2004
30th April 2008, 08:35
Except main thread, I have an other thread ,Which is checking a file's content, in my program. According to it's content for example there is character 'A' or 'a' in this file. I want to make my program work as if it is pressed 'A' button on keyboard.

wysota
30th April 2008, 10:19
Ok, but why? What is it meant to do with the key press? I mean what is the point of the wholeoperation?