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. Simple way to have radio buttons call same function with different parameter?
Forum Update on Monday, May 27th 2025

Simple way to have radio buttons call same function with different parameter?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.1k 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I'm creating a set of QRadioButton where each button is triggering essentially the same action but with a different value. For example:

          QRadioButton* bn1 = new QRadioButton("1");
          connect(bn1, &QRadioButton::clicked, this, &MyClass::setStation1);
          
          QRadioButton* bn2 = new QRadioButton("2");
          connect(bn2, &QRadioButton::clicked, this, &MyClass::setStation2);
                
          QRadioButton* bn2 = new QRadioButton("3");
          connect(bn3, &QRadioButton::clicked, this, &MyClass::setStation3);
    

    It would be convenient to replace each of the setStation*() slot methods with a single method setStation(int station) and just pass the index of the station. Is there a way to do this?

    JonBJ 1 Reply Last reply
    0
    • K kitfox

      I'm creating a set of QRadioButton where each button is triggering essentially the same action but with a different value. For example:

            QRadioButton* bn1 = new QRadioButton("1");
            connect(bn1, &QRadioButton::clicked, this, &MyClass::setStation1);
            
            QRadioButton* bn2 = new QRadioButton("2");
            connect(bn2, &QRadioButton::clicked, this, &MyClass::setStation2);
                  
            QRadioButton* bn2 = new QRadioButton("3");
            connect(bn3, &QRadioButton::clicked, this, &MyClass::setStation3);
      

      It would be convenient to replace each of the setStation*() slot methods with a single method setStation(int station) and just pass the index of the station. Is there a way to do this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @kitfox
      One way is to use a lambda to pass a parameter, like

      QRadioButton* bn1 = new QRadioButton("1");
      connect(bn1, &QRadioButton::clicked, this, [=] () { this->setStation(1); } );
      
      QRadioButton* bn2 = new QRadioButton("2");
      connect(bn2, &QRadioButton::clicked, this, [=] () { this->setStation(2); } );
      

      This example could also be done with QSignalMapper Class, but lambdas are more usual now.

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You could use lambdas for that.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • K kitfox

          I'm creating a set of QRadioButton where each button is triggering essentially the same action but with a different value. For example:

                QRadioButton* bn1 = new QRadioButton("1");
                connect(bn1, &QRadioButton::clicked, this, &MyClass::setStation1);
                
                QRadioButton* bn2 = new QRadioButton("2");
                connect(bn2, &QRadioButton::clicked, this, &MyClass::setStation2);
                      
                QRadioButton* bn2 = new QRadioButton("3");
                connect(bn3, &QRadioButton::clicked, this, &MyClass::setStation3);
          

          It would be convenient to replace each of the setStation*() slot methods with a single method setStation(int station) and just pass the index of the station. Is there a way to do this?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @kitfox
          One way is to use a lambda to pass a parameter, like

          QRadioButton* bn1 = new QRadioButton("1");
          connect(bn1, &QRadioButton::clicked, this, [=] () { this->setStation(1); } );
          
          QRadioButton* bn2 = new QRadioButton("2");
          connect(bn2, &QRadioButton::clicked, this, [=] () { this->setStation(2); } );
          

          This example could also be done with QSignalMapper Class, but lambdas are more usual now.

          1 Reply Last reply
          3
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #4

            There is also another old-fashioned way : getting data from the sender.

            void MyClass::setStation() {
                if(QRadioButton *button = qobject_cast<QRadioButton*>(sender())) {
                   QString text = button->text();
                    if(text == "1") {
                    } else if(text == "2") {
                    }
                }
            }
            

            But lambdas are way more flexible.

            JonBJ 1 Reply Last reply
            1
            • B Bonnie

              There is also another old-fashioned way : getting data from the sender.

              void MyClass::setStation() {
                  if(QRadioButton *button = qobject_cast<QRadioButton*>(sender())) {
                     QString text = button->text();
                      if(text == "1") {
                      } else if(text == "2") {
                      }
                  }
              }
              

              But lambdas are way more flexible.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @Bonnie
              There is, but IIRC (I think I read it somewhere) sender() == nullptr if slot called via lambda?

              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