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 22 Apr 2020, 20:42 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?

    J 1 Reply Last reply 22 Apr 2020, 20:57
    0
    • K kitfox
      22 Apr 2020, 20:42

      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?

      J Offline
      J Offline
      JonB
      wrote on 22 Apr 2020, 20:57 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 22 Apr 2020, 20:55 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
          22 Apr 2020, 20:42

          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?

          J Offline
          J Offline
          JonB
          wrote on 22 Apr 2020, 20:57 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 22 Apr 2020, 23:59 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.

            J 1 Reply Last reply 23 Apr 2020, 16:50
            1
            • B Bonnie
              22 Apr 2020, 23:59

              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.

              J Offline
              J Offline
              JonB
              wrote on 23 Apr 2020, 16:50 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

              1/5

              22 Apr 2020, 20:42

              • Login

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