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. using QKeyEvents (revisited)

using QKeyEvents (revisited)

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

    Hi all -

    A few months ago, I was experimenting with QKeyEvents, and got a lot of help here. That exercise was ultimately abandoned, but I'd like to revisit it now. So, some of this post may be redundant with my earlier ones.

    I'd like to use a keypress/release to show and hide a "secret" button on my main widget. Can I do this without creating a filter? I was thinking of something like this:

    void Widget::keyPressEvent(QKeyEvent *event)
    {
        int key = event->key();
        if (event->type() == QEvent::ShortcutOverride)
        {
            if ((key == 'd' || key == 'D'))
            {
                ui->pushButtonMcastServer->setVisible(true);
            }
        }
        else if (event->type() == QEvent::KeyRelease)
        {
            if ((key == 'd' || key == 'D'))
            {
                ui->pushButtonMcastServer->setVisible(false);
            }
        }
       // here I'd like to pass the event to the base class.
    }
    
    1. is this the correct/preferred method of doing this?
    2. how do I pass the event to the base class?

    Thanks...

    jsulmJ 1 Reply Last reply
    -1
    • mzimmersM mzimmers

      Hi all -

      A few months ago, I was experimenting with QKeyEvents, and got a lot of help here. That exercise was ultimately abandoned, but I'd like to revisit it now. So, some of this post may be redundant with my earlier ones.

      I'd like to use a keypress/release to show and hide a "secret" button on my main widget. Can I do this without creating a filter? I was thinking of something like this:

      void Widget::keyPressEvent(QKeyEvent *event)
      {
          int key = event->key();
          if (event->type() == QEvent::ShortcutOverride)
          {
              if ((key == 'd' || key == 'D'))
              {
                  ui->pushButtonMcastServer->setVisible(true);
              }
          }
          else if (event->type() == QEvent::KeyRelease)
          {
              if ((key == 'd' || key == 'D'))
              {
                  ui->pushButtonMcastServer->setVisible(false);
              }
          }
         // here I'd like to pass the event to the base class.
      }
      
      1. is this the correct/preferred method of doing this?
      2. how do I pass the event to the base class?

      Thanks...

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mzimmers

      1. I would say yes
      2. As explained in the documentation: calling base class keyPressEvent(), https://doc.qt.io/qt-5/eventsandfilters.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by mzimmers
        #3

        Thanks! For anyone who happens to look this up in the future, the code in my first post contains a few errors. The code posted below is working for me:

        void Widget::keyPressEvent(QKeyEvent *event)
        {
            if (event->key() == Qt::Key_Alt)
            {
                ui->pushButtonMcastServer->setVisible(true);
            }
            QWidget::keyPressEvent(event);
        }
        void Widget::keyReleaseEvent(QKeyEvent *event)
        {
            if (event->key() == Qt::Key_Alt)
            {
                ui->pushButtonMcastServer->setVisible(false);
            }
            QWidget::keyPressEvent(event);
        }
        

        (Note: I had to switch to a meta-key because the pressing and holding the "D" key caused lots of "D"s to be entered into random editable widgets.)

        1 Reply Last reply
        1

        • Login

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