Connect Many Signals to One Slot

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

  • Qt Champions 2018

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

    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.

  • @aha_1980 Thank you, I shall investigate this.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.