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
Servers for Qt installer are currently down

QShortcut multiple widget instances

Scheduled Pinned Locked Moved Solved General and Desktop
qshortcutmultiple instan
3 Posts 2 Posters 2.7k 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 6 Jun 2016, 20:47 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
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Jun 2016, 21:01 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.

      J 1 Reply Last reply 6 Jun 2016, 21:03
      6
      • C Chris Kawa
        6 Jun 2016, 21:01

        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.

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 6 Jun 2016, 21:03 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

        1/3

        6 Jun 2016, 20:47

        • Login

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