Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to receive KeyEvents on a QWidget?

How to receive KeyEvents on a QWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I'm designing a QWidget that I'm manually drawing via the paintEvent(). I'd like to be able to receive keypress events on it. I have a second object derived from QObject that has installed an eventFilter on my QWidget and while it is receiving various events (such as windows and mouse events), it is not receiving any keypress events even after I click on it and press keys on my keyboard. Is there anything else I need to do to capture keyboard input?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Check the QWidget::grabKeyboard and its friend QWidget::releaseKeyboard.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        Looks like the solution is to call setFocusPolicy(Qt::StrongFocus); in the constructor of the QWidget.

        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