Important: Please read the Qt Code of Conduct -

connect multiple no-arg signals to slot carrying enum information

  • I want to connect multiple QPushButton::release() signals to what is basically the same or a very similar slot. They are all intended to start a calculation carrying information about what exactly should be done. This is represented as an enum in my application as i.e. enum { CALC_3D, CALC_TEX, CALC_3DTEX }. How can I achieve this without duplicating code or generating 3 helper slots which thus far still seems like the best solution.
    I know that a QSignalMapper could do this but I'm not in love with mapping QPushButton's this pointers, strings or integers that later are converted to enum values. I.e. if I chance the order or naming of my enum values, this method will break and I won't notice before runtime.
    Is there any best practice for this? Can I for example already map the signal in a reasonable way?

  • Moderators

    You can use lambdas:

    connect(button1, &QPushButton::released, [=]{ someObject->someSlot(EnumValue1); });
    connect(button2, &QPushButton::released, [=]{ someObject->someSlot(EnumValue2); });

    Btw. it's more usual to connect to clicked signal and not released when it comes to buttons.

Log in to reply