Results 1 to 6 of 6

Thread: QKeyPress or ReleaseEvent

  1. #1
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QKeyPress or ReleaseEvent

    Hi.
    I reimplemented keyPressEvent and keyReleaseEvents functions.Iam using F1,F2,F3 buttons.
    User will use f1 for page_down and f2 for page_up buttons.So I need to create this events and send them?

    I created these.
    QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
    QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_Up, Qt::NoModifier);

    and

    switch(key)
    {
    case Qt::Key_F1:
    e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
    result=QWidget::event(e);
    break;
    case Qt::Key_F2:
    e = new QKeyEvent(QEvent::KeyPress, Qt::Key_PageUp, Qt::NoModifier);
    result=QWidget::event(e);
    break;
    }

    wrote this function.But QWidget::event(e) didnt answer my action.To which function will I send this event?

  2. #2
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QKeyPress or ReleaseEvent

    When you capture the F1 or F2 key, why don't you just tell it to run the code Page_Up and Page_Down run? If you have to create the event and run the event, the reason you may not see the Page_Up and Page_Down happening is because you have over-ridden the keyPressEvent function and you don't do anything when you receive the Page_Up and Page_Down keys. So you will have to tell those keys what to do which takes you back to the first sentence I wrote, just tell the F1 and F2 keys what to do instead of making them create Page_Up and Page_Down events.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QKeyPress or ReleaseEvent

    User will use f1 for page_down and f2 for page_up buttons.So I need to create this events and send them?
    No you don't - they are defined by Qt already.
    You only need to use them.

    I created these.
    QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_PageDown, Qt::NoModifier);
    QKeyEvent *e = new QKeyEvent(QEvent::KeyPress,Qt::Key_Up, Qt::NoModifier);
    Where? and how do you use them?

    If you want to catch the keyPressEvent in a widget you should reimplement the keyPressedEvent() method so:
    Qt Code:
    1. void MyWidget::keyPressEvent ( QKeyEvent * event )
    2. {
    3. switch(evet->key())
    4. {
    5. case Qt::Key_F1: //do something
    6. accept(); //we want to handle this
    7. break;
    8. case Qt::Key_F2: //do something else
    9. accept(); //we want to handle this
    10. break;
    11. default: ignore(); //let others handle this
    12.  
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    its all in the docs:
    http://doc.trolltech.com/4.2/qwidget.html#keyPressEvent
    Widgets that accept keyboard input need to reimplement a few more event handlers:

    * keyPressEvent() is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat. Note that the Tab and Shift+Tab keys are only passed to the widget if they are not used by the focus-change mechanisms. To force those keys to be processed by your widget, you must reimplement QWidget::event().

  4. #4
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QKeyPress or ReleaseEvent

    I know.I have already reimplemented this function.Iam catching f1,f2,f3 presses too.But subject is that:
    My keyboard on embedded device has not page_down and page_up buttons.So user will use f1 and f2 keys for this.I know that Qt event system can control these page_up and page_down keys automatically.I want to create QKeyEvents for this keys.And created.
    void ProgramList::keyReleaseEvent(QKeyEvent *event)
    {

    Qt::Key key =(Qt::Key)event->key();
    e=NULL;
    bool result;

    if(event->isAutoRepeat()==false)
    {
    switch(key)
    {
    case Qt::Key_F1:
    e = new QKeyEvent(QEvent::KeyRelease,Qt::Key_PageDown, Qt::NoModifier);
    result=QWidget::event(e);
    break;
    case Qt::Key_F2:
    e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_PageUp, Qt::NoModifier);
    result=QWidget::event(e);
    break;
    case Qt::Key_F3:
    emit setIndex(UIprogramList->treeWidget->indexOfTopLevelItem(UIprogramList->treeWidget->currentItem()));
    break;
    }
    }
    }


    When released f1 and f2 my function will catch it.Then I will create an event "e".It is not f1 or f2 realased event.It is an page-up or page-down event and I am sending it to QWidget::event() function.
    I want to ask that:My function doesnt catch Key_PageUp and Key_PageDown.But which function catches this?QWidget::event() ?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QKeyPress or ReleaseEvent

    I still don't understand why do you create new events since you ALWAYS create new page up or down events on F1 for example, just do your handling when you catch F1.
    But it could be I am still not getting right what you are doing.

    I want to ask that:My function doesnt catch Key_PageUp and Key_PageDown.But which function catches this?QWidget::event() ?
    Well, there is not case to catch Key_PageUp or Key_PageDown in your code - you should add them to the case switch in order to catch them.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QKeyPress or ReleaseEvent

    Either tell F1 to do what you want the Page_Up to do and tell the F2 to tell the Page_Down what to do.

    The other option is if you must create these unnecessary events, then you will have to capture them in the same keyPressEvent handler you create adding 2 new cases to your switch and then put the code for Page_Up and Page_Down in this same event handler. This would be silly to capture 2 events to do 1 thing when you could just tell F1 and F2 what to do instead of having them create events to do the tasks.

    Are you trying to pass the Page_Up and Page_Down events to different widgets? If so, then you should just have those widgets capture the F1 and F2 keys so the widget you want receiving the Page_Up and Page_Down commands capture the F1 and F2 commands.

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.