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. QShortcut multiple widget instances
Forum Update on Monday, May 27th 2025

QShortcut multiple widget instances

Scheduled Pinned Locked Moved Solved General and Desktop
qshortcutmultiple instan
3 Posts 2 Posters 2.6k 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by
    #1

    I have a custom QWidget that deploys QShortcuts for the Ctrl+F and Escape key sequences. Everything works great as long as I have just one instance of that custom widget. However, as soon as I create multiple instances none of the widgets react on the key sequences anymore.
    I would have suspected that the widget that has focus gets the key events and therefore that widgets QShortcuts are "enabled". Apparently I am wrong.

    What is the proper solution to handle this problem?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Two things are important when dealing with shortcuts: shortcut parent and context. By default a shortcut has window context so you can't have two of them in different widgets inside that same window. You have to set it explicitly to something narrower, e.g. Qt::WidgetWithChildrenShortcut. With this setting it's important to give the shortcuts appropriate parents to scope them.

      auto sh1 = new QShortcut(QKeySequence::Find, someWidget1, SLOT(whatever()), nullptr, Qt::WidgetWithChildrenShortcut);
      auto sh2 = new QShortcut(QKeySequence::Find, someWidget2, SLOT(iDontCare()), nullptr, Qt::WidgetWithChildrenShortcut);
      

      this way sh1 will work inside someWidget1 and sh2 inside someWidget2.

      Joel BodenmannJ 1 Reply Last reply
      6
      • Chris KawaC Chris Kawa

        Two things are important when dealing with shortcuts: shortcut parent and context. By default a shortcut has window context so you can't have two of them in different widgets inside that same window. You have to set it explicitly to something narrower, e.g. Qt::WidgetWithChildrenShortcut. With this setting it's important to give the shortcuts appropriate parents to scope them.

        auto sh1 = new QShortcut(QKeySequence::Find, someWidget1, SLOT(whatever()), nullptr, Qt::WidgetWithChildrenShortcut);
        auto sh2 = new QShortcut(QKeySequence::Find, someWidget2, SLOT(iDontCare()), nullptr, Qt::WidgetWithChildrenShortcut);
        

        this way sh1 will work inside someWidget1 and sh2 inside someWidget2.

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #3

        Thank you Sir, this helped a lot!

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        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