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. How to have QWidget keypress override main window menu shortcuts?

How to have QWidget keypress override main window menu shortcuts?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 962 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by kitfox
    #1

    I'm writing a program where I'd like to have a custom widget override the keypress behavior of the main menu actions.

    When it program starts, it sets up it's main menu using QActions:

        QMenuBar* mb = new QMenuBar();
        
        QMenu* editMenu = mb->addMenu(tr("&Edit"));
    
        {    
            QAction* action = new QAction(tr("&Delete"), _mainWindow);
            action->setShortcut(QKeySequence("Delete"));
            action->setStatusTip(tr("Delete selection"));
            connect(action, &QAction::triggered, this, &Environment::deleteSelection);
            actionMap[ACTION_DELETE] = action;
        
            editMenu->addAction(actionMap[ACTION_DELETE]);
        }
        //More entries like this
        
        _mainWindow->setMenuBar(mb);
    

    My custom widget needs to do something different when Delete is pressed. I've overridden keyPressEvent, and while keyPressEvent is being called for most keypresses, any key combo that the maim menu actions have been assigned to is not making it into this method. Is there any way to have my widget handle the keypress first, and then only pass it on to the main window if it decides it doesn't need it?

        void CurvesEditor::keyPressEvent(QKeyEvent *event)
        {
            switch (event->key())
            {
            case Qt::Key_Delete:
            {
                //Deletion code
                return;
            }
            }
            
            QWidget::keyPressEvent(event);
        }
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      In your widget override event(), check event type() and handle QEvent::ShortcutOverride to accept the event when the key sequence is QKeySequence::Delete.

      1 Reply Last reply
      5
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • K Offline
          K Offline
          kitfox
          wrote on last edited by
          #4

          That worked. 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