Results 1 to 12 of 12

Thread: Producing Keypressed Event

  1. #1
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Producing Keypressed Event

    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?

  2. #2
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Producing Keypressed Event


  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Producing Keypressed Event

    Qt Code:
    1. QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, Qt::Key_0 ,Qt::NoModifier);
    2. qApp->postEvent(objectThatHandlesKeypresses, ev);
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Producing Keypressed Event

    I tried to do this but it didnt work? I wrote it to main function.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Producing Keypressed Event

    Quote Originally Posted by anafor2004 View Post
    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?
    J-P Nurmi

  6. #6
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Producing Keypressed Event

    Qt Code:
    1. QApplication a(argc, argv);
    2. gkdpr w;
    3. QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress, Qt::Key_0 ,Qt::NoModifier);
    4. QApplication::sendEvent(w,ev);
    5. w.setGeometry(0,0,800,480);
    6. //w.setWindowFlags(Qt::FramelessWindowHint);
    7. w.show();
    8. a.installTranslator(&w.translator);
    9. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    10. return a.exec();
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Producing Keypressed Event

    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'

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Producing Keypressed Event

    Why don't you just trigger() the QAction object asociated with the menu entry?

  9. #9
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Producing Keypressed Event

    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?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Producing Keypressed Event

    What do you need those events for? What is the goal of your piece of code?

  11. #11
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Producing Keypressed Event

    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.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Producing Keypressed Event

    Ok, but why? What is it meant to do with the key press? I mean what is the point of the wholeoperation?

Similar Threads

  1. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39
  2. Replies: 1
    Last Post: 16th October 2007, 22:41
  3. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.