Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Keyboard events inside a QQuickPaintedItem/QQuickItem
Forum Updated to NodeBB v4.3 + New Features

Keyboard events inside a QQuickPaintedItem/QQuickItem

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mattropolis
    wrote on last edited by
    #1

    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) {...}
    @

    1 Reply Last reply
    0
    • F Offline
      F Offline
      froncer
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtTester
        wrote on last edited by
        #3

        you need to call setFlag(ItemAcceptsInputMethod, true);

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved