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. [SOLVED] help with keyPressEvent and focus
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] help with keyPressEvent and focus

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 5.5k 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.
  • P Offline
    P Offline
    pmh4514
    wrote on last edited by
    #1

    Hi,

    I've overridden keyPressEvent in a MainWindow to trap the 4 arrow keys. If I click my mouse anywhere in an unpopulated area of the window to give it focus, then the 4 arrow keys are trapped and handled as desired.

    However if I then click any of the buttons contained within the window, my arrow keys then start traversing the child controls, setting focus to each one through the tab order.

    How can I keep it so that the window traps the arrow keys and they don't end up scrolling through the child controls?

    Here is my handler, and I found it behaves the same regardless if I do or do not pass the event up to QMainWindow on the last line. As well, I call setFocusPolicy(Qt::StrongFocus); in the constructor.

    @
    void StageMotorsControlWin::keyPressEvent(QKeyEvent *keyevent)
    {
    if (keyevent->key()==Qt::Key_Left)
    {
    on_buttonXY_W_clicked();
    }

    if (keyevent->key()==Qt::Key_Left)
    {
        on_buttonXY_W_clicked();
    }
    
    if (keyevent->key()==Qt::Key_Up)
    {
        on_buttonXY_N_clicked();
    }
    
    if (keyevent->key()==Qt::Key_Right)
    {
        on_buttonXY_E_clicked();
    }
    
    if (keyevent->key()==Qt::Key_Down)
    {
        on_buttonXY_S_clicked();
    }
    QMainWindow::keyPressEvent(keyevent); // with or without this it's not quite what I'd like.
    

    }

    @

    Thanks!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      What I have understood from your explanation is that.

      Case #1. You have main window.
      Case #2. You have area with few buttons.
      Case #3. You have area without buttons.

      If you click on case #2, keys are taken by the buttons.
      If you click on case#2, keys are handled properly.

      If that is the case, you can install the eventFilters on your buttons and filter out the arrow key events. You can look at the installEventFilter and eventFilter(...) methods of QObject.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pmh4514
        wrote on last edited by
        #3

        Case #1. You have main window.
        Case #2. You have area with few buttons.
        Case #3. You have area without buttons.

        Yes, all true.

        If I click anywhere in Case #3 (or MainWindow title bar) the MainWindow traps the arrow keys and does as needed - no buttons or other form elements gain focus (as desired)

        If I click on any of the buttons in Case #2, that button takes focus. At that point, my arrow keys cause focus to traverse the buttons on the form as if I were pressing TAB on the keyboard.

        I don't want arrows to ever "hit" the buttons, to be always trapped at the MainWindow level regardless if I've clicked any of the buttons on the form.

        Really the only way is to put an event filter on all buttons? I can't trap it at the top level MainWindow?

        Thanks

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          If you put the install filters for the button, your keys won't reach the buttons at all. Since you are telling that events reaching the buttons, they are handled then and there. They don't propagate to parent. You need to install the filters so that events reach the filter object first. You can make the MainWindows itself as the event filter the button. You can filter those events before reaching to buttons.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pmh4514
            wrote on last edited by
            #5

            so I think you're saying that my overriding keyPressEvent(QKeyEvent *keyevent) within my MainWindow class, or even making an eventFilter at the main window level, will not work? I have to put an event filter on each button to prevent them from hitting
            (I guess I was thinking that the event hits the main window first, and then goes to the children, but it sounds like it's the other way around)

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              What I'm saying is that Install some QObject as event filter for Buttons. We have too much of english and imagination. It will really if you provide sample source code. We can make it work and give you.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pmh4514
                wrote on last edited by
                #7

                Actually you are making sense. It all comes down to using an eventFilter rather than simply overriding the event in the window. I can work with this, thanks!

                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