Stop propagating key event



  • Hi all;
    Is there an ability to stop propagating key event to parent item?
    By calling event.accepted = true, the default action is no longer working;
    For example, i have textEdit element, and when clicking the arrow_up button, cursor moves to the previous line of the text if exists, and if not exists parent item receives event;
    Is it possible to leave default action on press and stop propagating event?
    Thanks.



  • If i have understood correctly your question, on way is to intercept the event directly. For ex in your button, you put some onMouseClicked : {}, but then inside you have to put the code to make the cursor go upper if necessary.
    Normally in that case the parent wont have any event propagated.



  • You can do such things by subclassing the widget. For a QTextEdit e.g.:

    @
    void MyTextEdit::keyPressEvent ( QKeyEvent * event )
    {
    QTextEdit::keyPressEvent(event); // do default actions
    event->setAccepted(true); // prevent processing in the parent
    }
    @



  • As we are in a QML section, my response was a QML one, perhaps without any connection with the question, i agree, but may i suggest to change the section ?



  • Ups, sorry, I haven't looked at the sub forum :-(
    But perhaps qml has similar things?

    You have a perent element and add an event handler there which is empry or sets accepted to true.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.