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. keyPressEvent() isn't being called
QtWS25 Last Chance

keyPressEvent() isn't being called

Scheduled Pinned Locked Moved Solved General and Desktop
keypressevent
5 Posts 2 Posters 3.8k Views
  • 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.
  • A Offline
    A Offline
    AlaaM
    wrote on 2 Dec 2015, 10:03 last edited by AlaaM 12 Feb 2015, 10:24
    #1

    In my main.cpp I declare a MainWindow object, and show it:

    MainWindow mainWindow;
    mainWindow.showWindow();
    

    This object is a screen with 4 buttons (say screen 1). I connected the buttons with the Enter key so once clicked they open a new screen (2):

    connect(buttons[0], SIGNAL (released()), this, SLOT (openExample()));
    

    In that new screen I can either press ESC and return to the previous screen (which works by using installEventFilter() and implementing eventFilter()), or press ENT and go to the next new screen (3).

    That last thing is not working for me (pressing ENT in screen 2 to go to screen 3).

    This is what I've done:

    Screen 3:

    ExampleSettings::ExampleSettings(QWidget *parent)
                    : Settings(parent)
    {
        QVBoxLayout* exampleSettingsLayout = new QVBoxLayout(&widget);
    
        for(int i=0; i<BUTTONS_NUMBER; i++)
        {
            buttons[i] = new QLabel();
        }
        buttons[0]->setText(tr("SETTING 1"));
        buttons[1]->setText(tr("SETTING 2"));
    
        for(int i=0; i<BUTTONS_NUMBER; i++)
        {
            exampleSettingsLayout->addWidget(buttons[i]);
        }
    
        exampleSettingsLayout->installEventFilter(this);
    }
    

    and declared keyPressEvent():

    public:
        void keyPressEvent(QKeyEvent *event);
    

    and defined it:

    void ExampleSettings::keyPressEvent(QKeyEvent *event)
    {
        cout << "got event!" << endl;
    }
    

    but it's not being called.

    I think the problem is in installing the event filter. Maybe because exampleSettingsLayout is dead after the constructor finishes? but I also tried to make it a member variable of the class, and it didn't work (keyPressEvent() still not called).

    edit:
    I also tried eventFilter() instead of keyPressEvent(), but also this one isn't invoked.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Dec 2015, 23:36 last edited by SGaist 12 Feb 2015, 23:37
      #2

      Hi,

      It's uncommon to install an event filter on a layout. It's the widgets that usually get keyboard focus thus the key related events.

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

      A 1 Reply Last reply 3 Dec 2015, 15:31
      1
      • A Offline
        A Offline
        AlaaM
        wrote on 3 Dec 2015, 15:07 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • S SGaist
          2 Dec 2015, 23:36

          Hi,

          It's uncommon to install an event filter on a layout. It's the widgets that usually get keyboard focus thus the key related events.

          A Offline
          A Offline
          AlaaM
          wrote on 3 Dec 2015, 15:31 last edited by AlaaM 12 Mar 2015, 15:32
          #4

          @SGaist

          I see.
          Indeed widget.installEventFilter(this); worked, but with eventFilter() and not keyPressEvent().
          I don't understand how eventFilter() is activated and the condition if (event->type() == QEvent::KeyPress) is met, but on the other hand keyPressEvent() isn't invoked.
          The disadvantage with eventFilter() is that it's always triggered. Can anything be done to trigger only keyPressEvent()?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 3 Dec 2015, 21:45 last edited by
            #5

            Well, that's the use of eventFilter. In your reimplementation, you check only for what you are interested in and when it's not just call the base class implementation.

            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

            1/5

            2 Dec 2015, 10:03

            • Login

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