Keyboard events inside a QQuickPaintedItem/QQuickItem



  • Hi all,

    I was writing an OpenGL widget (which is working fine), and I got mouse events working great - but am having a terrible time getting keyboard events to fire/register. I have overridden keyPressEvent/keyReleaseEvent, but when I click on the widget (widget is placed/loaded in a QML script), the mouse events fire, but no keyboard events seem to fire in the keyPress/ReleaseEvent callbacks.

    I noted that I had to use setAcceptedMouseButtons() in order to get mouse events to work, is there any equivalent for keyboard events that I should be setting?
    Am I missing something? The docs seem to indicate setting the focus policy, but setFocusPolicy() doesn't work on QtQuick items.

    Any help would be greatly appreciated!

    glViewer.h
    @
    class glViewer:public QQuickPaintedItem
    {
    Q_OBJECT

    public:
    glViewer();
    ~glViewer();
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent * event);
    void wheelEvent(QWheelEvent *event);
    void keyPressEvent(QKeyEvent event);
    void keyReleaseEvent(QKeyEvent
    keyEvent);
    ...

    public slots:
    void preRender();
    void paint(QPainter *painter);
    void selectedBufferAttributeIndexUpdate();

    ...
    }
    @

    glViewer.cpp
    @
    glViewer::glViewer()
    {
    // set the drawing render target to a paint FBO
    setRenderTarget(QQuickPaintedItem::FramebufferObject);

    // enable mouse events
    setAcceptedMouseButtons(Qt::AllButtons);
    setAcceptHoverEvents(true);
    }

    void glViewer::mousePressEvent(QMouseEvent *event) { ...}
    void glViewer::mouseReleaseEvent(QMouseEvent *event) {...}
    ...
    void glViewer::keyPressEvent(QKeyEvent event) {...}
    void glViewer::keyReleaseEvent(QKeyEvent
    keyEvent) {...}
    @



  • I can receive key events, when I set the focus:true property in the QML script and overload the focusInEvent() function, where I set forceAtiveFocus().

    @
    ..
    void TreeViewControl::focusInEvent(QFocusEvent * event)
    {
    forceActiveFocus();
    }
    ..
    @

    @
    MyTreeViewControlArea
    {
    focus:true
    anchors.top: parent.top
    anchors.left: parent.left
    width: parent.width
    height: parent.height
    opacity: 0.8
    }
    @

    You can find some information about focus handling in QtQuick here:
    http://qt-project.org/doc/qt-5/qtquick-input-focus.html


Log in to reply
 

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