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. Why keyPressEvent doesn't work?
Qt 6.11 is out! See what's new in the release blog

Why keyPressEvent doesn't work?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 8.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.
  • L Offline
    L Offline
    lucadanieli
    wrote on last edited by
    #1

    Hi everyone.

    I wrote a simple eventFilter, and it works well but I don't understand why a keyPressEvent isn't working.
    Indeed, the case Key_Tab works perfectly, but the case Key_Backtab doesn't.
    I have testing it and the problem resides on keyPressEvent(keyEvent).
    My code is associated with a QComboBox, and particularly it lightens rows forwards but not backwards (in this case the new row is not visually updated).

    @bool eventFilter(QObject *t, QEvent *e)
    {
    QKeyEvent keyEvent = static_cast<QKeyEvent>(e);
    int key = keyEvent->key();

        if(e->type() == QEvent::KeyPress)
        {
            switch(key)
            {
                case Qt::Key_Backtab:
                //case Qt::Key_Up:
                {
                    int curIndex = this->currentIndex();
                    int nextIndex;
    
                    if (this->currentIndex() == 0)
                        nextIndex = this->count() -1;
                    else {
                        nextIndex = curIndex - 1;
                    }
    
                    this->setCurrentIndex(nextIndex);
    
                    QComboBox::keyPressEvent(keyEvent);
    
                    break;
                }
                case Qt::Key_Tab:
                //case Qt::Key_Down:
                {
                    int curIndex = this->currentIndex();
                    int nextIndex;
    
                    if (this->currentIndex() == this->count() -1 )
                        nextIndex = 0;
                    else {
                        nextIndex = curIndex + 1;
                    }
    
                    this->setCurrentIndex(nextIndex);
    
                    QComboBox::keyPressEvent(keyEvent);
    
                    break;
                }
            }
        }@
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I wonder if you should not rather test for the shift modifier + Key_Tab (haven't tested it)

      Hope it helps

      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
      0
      • L Offline
        L Offline
        lucadanieli
        wrote on last edited by
        #3

        I'll test it, but actually the "case statement" works, asf I can confirm that setCurrentIndex works.

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

          I didn't understand that phrase. Do you mean that your code is currently working ?

          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
          0
          • L Offline
            L Offline
            lucadanieli
            wrote on last edited by
            #5

            Yep, but not perfectly. In my opinion only QComboBox::keyPressEvent(keyEvent) in the Backtab section doesn't work.

            My code is thought like this.
            I have a comboBox connected to an editor which contains a document. Changing the comboBox's index, the document changes. So, pressing Key_Tab to move forward:

            document_1 -> document_2 -> document_3
            hlight_row_1 -> hlight_row_2 -> hlight_row_3

            This works perfectly.
            Instead, if I press Key_Backtab (backward), the behaviour is:

            document_1 -> document_3 -> document_2 --------//which is right
            hlight_row_1 -> hlight_row_1 -> hlight_row_1 -------//stays still

            the code in the Backtab section, then, seems to be executed, because my documents in the editor are changing backwards, but just the highlighting of the respective rows inside the comboBox popup list doesn't.
            Deleting the QComboBox::keyPressEvent(keyEvent) line, I obtain the same exact behaviour. So I thought that for some reason keyPressEvent wasn't work.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              are you sure that the Key_Backtab key is not used or controlled by something else?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lucadanieli
                wrote on last edited by
                #7

                I am not sure, but if I just delete both these lines from the Backtab case:

                @ this->setCurrentIndex(nextIndex);

                                QComboBox::keyPressEvent(keyEvent);
                

                @
                nothing happens any more, and neither documents are any more changing backwards in the Editor (instead, the Tab case still works): completely frozen.

                So I don't think it is used by anything else, also considering that I have focus on the menu list.

                Is it possible that Backtab (recognized as modifier + tab) wants keyEvent->accept()?

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Why do not you try to do the opposite?
                  that is, to call a function keypress (which will change the editor or combobox)?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lucadanieli
                    wrote on last edited by
                    #9

                    Thank Salvatello for your suggestion. Did you mean to override the default keyPressEvent? Like this:

                    @void keyPressEvent(QKeyEvent *e)
                    {
                    int key = e->key();

                        switch(key)
                        {
                            case Qt::Key_Backtab:
                            //case Qt::Key_Up:
                            {
                                int curIndex = this->currentIndex();
                                int nextIndex;
                    
                                if (this->currentIndex() == 0)
                                    nextIndex = this->count() -1;
                                else {
                                    nextIndex = curIndex - 1;
                                }
                    
                                this->setCurrentIndex(nextIndex);
                    
                                break;
                    
                            }
                            case Qt::Key_Tab:
                            //case Qt::Key_Down:
                            {
                                 int curIndex = this->currentIndex();
                                 int nextIndex;
                    
                                 if (this->currentIndex() == this->count() -1 )
                                      nextIndex = 0;
                                 else {
                                      nextIndex = curIndex + 1;
                                 }
                    
                                 this->setCurrentIndex(nextIndex);
                    
                                 break;
                            }
                        }
                    }
                    
                    bool eventFilter(QObject *t, QEvent *e)
                    {
                        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);
                        int key = keyEvent->key();
                    
                        if(e->type() == QEvent::KeyPress)
                        {
                            this->keyPressEvent(keyEvent);
                        }
                    

                    @

                    This code still changes documents in the editor, but it doesn't move the highlight in the menu list forwards and backwards any more.

                    If I add

                    @ QComboBox::keyPressEvent(e); @

                    just after the switch function, I obtain the same behaviour as before, so: documents changed in both directions; selection highlights only if forward direction.

                    Therefore now my question is more specific: should setCurrentIndex() automatically change the highlight in the menu list to the specific row called?
                    (If I am not wrong: when calling QComboBox::keyPresseEvent() what I do is asking for the default behaviour of the comboBox relative to Key_Tab. Maybe comboBox has not default behaviour for Key_Backtab? How to lighten manually a row then?)

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      I suggest:
                      @void MainWindows::keyPressEvent(QKeyEvent *e)

                      {

                      if (e->type() == QEvent::KeyPress)
                      

                      {
                      QKeyEvent keyEvent = static_cast<QKeyEvent>(e);

                      int keyInt = keyEvent->key();
                      
                      
                      
                      Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
                      

                      //For combination of button
                      if(modifiers & Qt::ShiftModifier)
                      keyInt += Qt::SHIFT;
                      if(modifiers & Qt::ControlModifier)
                      keyInt += Qt::CTRL;
                      if(modifiers & Qt::AltModifier)
                      keyInt += Qt::ALT;
                      if(modifiers & Qt::MetaModifier)
                      keyInt += Qt::META;

                      if(keyInt==Qt::Key_Backtab)
                          on_spacePushButton_Backtab();
                      if(keyInt==Key_Tab)
                          on_pushButtonTab();
                      
                      }
                      

                      }

                      void MainWindows::on_spacePushButton_Backtab()
                      {
                      .......................
                      ........................
                      }

                      void MainWindows::on_pushButtonTab()
                      {
                      .......................
                      ........................
                      }

                      @

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lucadanieli
                        wrote on last edited by
                        #11

                        In this case Backtab is not recognized any more by my machine. And therefore the if statement doesn't compile.

                        But any way, I think that I expressed badly my problem. My keyPress already works. My problem is simply that:

                        when I setCurrentIndex() in my comboBox, in case the event is Backtab, the highlight in the comboBox doesn't move.

                        It is simply just the highlight, because all the rest works. Even setCurrentIndex() I am sure works. But:

                        Tab -> highlight (in comboBox) moves forwards
                        Backtab -> highlight doesn't move

                        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