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 subclass works only in debug
Qt 6.11 is out! See what's new in the release blog

keyPressEvent subclass works only in debug

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 923 Views 2 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
    Mark81
    wrote on last edited by Mark81
    #1

    I'm not sure what I'm doing wrong here.
    In my QDialog form I want to customize the pressing of some keys, like esc.
    So I wrote:

    DialogComponents.h

    protected:
        void keyPressEvent(QKeyEvent *e);
    

    DialogComponents.cpp

    void DialogComponents::keyPressEvent(QKeyEvent *e)
    {
        qDebug() << e->key();
        if (e->key() == Qt::Key_Escape) emit swapCurrentStack();
        else QDialog::keyPressEvent(e);
    }
    

    if I run the debugger (run a debug build is not enough) it works like expected:

    1. it prints the key() value
    2. if I press key is esc it emits the signal and executes the connected slot
    3. if I press another key, i.e. enter the default behavior is unchanged

    but if I run the application (even built as debug) without attach the debugger, it just does nothing: no qDebug() and no effect at all. Of course all other qDebug() I have work fine.

    The mistake is in the code above?

    aha_1980A 1 Reply Last reply
    0
    • M Mark81

      I'm not sure what I'm doing wrong here.
      In my QDialog form I want to customize the pressing of some keys, like esc.
      So I wrote:

      DialogComponents.h

      protected:
          void keyPressEvent(QKeyEvent *e);
      

      DialogComponents.cpp

      void DialogComponents::keyPressEvent(QKeyEvent *e)
      {
          qDebug() << e->key();
          if (e->key() == Qt::Key_Escape) emit swapCurrentStack();
          else QDialog::keyPressEvent(e);
      }
      

      if I run the debugger (run a debug build is not enough) it works like expected:

      1. it prints the key() value
      2. if I press key is esc it emits the signal and executes the connected slot
      3. if I press another key, i.e. enter the default behavior is unchanged

      but if I run the application (even built as debug) without attach the debugger, it just does nothing: no qDebug() and no effect at all. Of course all other qDebug() I have work fine.

      The mistake is in the code above?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mark81 said in keyPressEvent subclass works only in debug:

      protected:
      void keyPressEvent(QKeyEvent *e);

      You can add override to make sure you really re-implement the base implementation:

      void keyPressEvent(QKeyEvent *e) override;

      Otherwise I don't see what could be wrong with your code.

      Regards

      Qt has to stay free or it will die.

      M 1 Reply Last reply
      0
      • aha_1980A aha_1980

        @Mark81 said in keyPressEvent subclass works only in debug:

        protected:
        void keyPressEvent(QKeyEvent *e);

        You can add override to make sure you really re-implement the base implementation:

        void keyPressEvent(QKeyEvent *e) override;

        Otherwise I don't see what could be wrong with your code.

        Regards

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @aha_1980 well, the override keyword does nothing, apparently.
        But I found a more specific criteria for the weird behavior.

        Now I can say the wrong behavior is not due to the debugger.
        Sorry for the misleading title.

        When I run the application the keyPressEvent is not fired until I minimize and restore the window.
        I don't know why.

        Some other stuff that might be related.
        I also implemented the showEvent function to update the QTableView model when the dialog is shown:

        void DialogComponents::showEvent(QShowEvent *event)
        {
            Q_UNUSED(event)
            qDebug() << "show event fired";
            ui->table->clearSelection();
            _model->select();
        }
        

        but this fires anyway, even when I open it the first time, of course.

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

          Hi,

          Are you sure your dialog has the keyboard focus and not one of the widget inside of it ?

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

          M 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            Are you sure your dialog has the keyboard focus and not one of the widget inside of it ?

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @SGaist said in keyPressEvent subclass works only in debug:

            Are you sure your dialog has the keyboard focus and not one of the widget inside of it ?

            It should - at least I assume that - because if I remove the override function, when I press esc the QDialog closes (actually, rejects) under the same conditions.

            1 Reply Last reply
            0
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              What version of Qt are we talking about, and what OS?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                Qt 5.12.2, Windows 7

                J.HilkJ 1 Reply Last reply
                0
                • M Mark81

                  Qt 5.12.2, Windows 7

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Mark81 I would suggest to update to at least the latest version of 5.12, as it is a LTS version.

                  Also you know, that support for win 7 will be close to non existent come January ?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • qwasder85Q Offline
                    qwasder85Q Offline
                    qwasder85
                    wrote on last edited by
                    #9

                    My guess is the keyboard focus. Try setFocus() on your dialog when it opens.

                    M 1 Reply Last reply
                    1
                    • qwasder85Q qwasder85

                      My guess is the keyboard focus. Try setFocus() on your dialog when it opens.

                      M Offline
                      M Offline
                      Mark81
                      wrote on last edited by
                      #10

                      @qwasder85 said in keyPressEvent subclass works only in debug:

                      My guess is the keyboard focus. Try setFocus() on your dialog when it opens.

                      Yes, it's definitely related to the focus. It's strange that the default event works in any case.

                      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