Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect Many Signals to One Slot
Qt 6.12 LTS is out! See what's new in the release blog

Connect Many Signals to One Slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 4.7k 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    I have a QWidget containing many QPushButtons. Each QPushButton has a dynamic property attached whose value is specific to that button.

    On all of these buttons, I want the 'clicked' signal to connect to the same slot - then within the slot, I can use QObject::sender() to identify which button emitted the clicked signal by inspecting the dynamic property.

    Is there a quick and convenient way to connect many signals (i.e the QPushButtons connect) to one slot, instead of having a large number of 'connect' statements?

    Lazy, absolutely, but I'm curious.

    1 Reply Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @webzoid: There is indeed a Qt class you can use for this task: QSignalMapper.

      http://doc.qt.io/qt-5/qsignalmapper.html

      Edit: You will still have some code for the connects, but in the slot you can identify which button was pressed. If you buttons are in an array, you can connect them in a loop.

      Qt has to stay free or it will die.

      webzoidW 1 Reply Last reply
      2
      • aha_1980A aha_1980

        @webzoid: There is indeed a Qt class you can use for this task: QSignalMapper.

        http://doc.qt.io/qt-5/qsignalmapper.html

        Edit: You will still have some code for the connects, but in the slot you can identify which button was pressed. If you buttons are in an array, you can connect them in a loop.

        webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

        @aha_1980 Thank you, I shall investigate this.

        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
        • Unsolved