SetMapping of QRadioButtons



  • I have a list of 32 radio buttons,I have this program:

    ui->scrollArea->setWidget(ui->scrollAreaWidgetContents);
        ui->scrollArea->verticalScrollBar()->setSingleStep(30);
        for(int i=0;i<32;i++){
    
    
                    radio_name.clear();
                    radio_name.append("RadioButton_");
                    radio_name.append(QString::number(i+1));
    
                    radioButtons[i]=new QRadioButton(ui->scrollAreaWidgetContents);
                    radioButtons[i]->setObjectName(QString::fromUtf8(radio_name.toStdString().c_str()));
                   radioButtons[i]->setProperty("DeviceIndex", i +1);
    }
    ui->scrollAreaWidgetContents->setGeometry(0,0,480,960);
    
         QList<QRadioButton *> allPButtons = this->findChildren<QRadioButton *>();
    
         foreach (QRadioButton *button, allPButtons)
         {
             keyboardMapper->setMapping(button, button);
    
             connect(button, SIGNAL(clicked(bool)), keyboardMapper, SLOT(map()));
         }
    
    

    the problem is on the instruction

     keyboardMapper->setMapping(button, button);
    

    How can I fix it??


  • Moderators

    @slimhs Instead of mapping you can use this new signal-slot connection.

    connect(radioButton, &QRadioButton::clicked, [=](bool checked){
       qDebug() << button << checked; //the exact button with state 
    });
    

    You can add this in for loop where you create new RadioButtons.
    Note that this requires c++11 compliant compiler.



  • where do you construct keyboardMapper?

    also, for what you are trying to do there is QObject::sender which is bad programming but it works


Log in to reply
 

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