[Solved] Programatically check the RadioButton inside a GroupBox



  • Hi,

    I created a set of RadioButtons inside a GroupBox as below.

    @QSignalMapper *size_signal_mapper = new QSignalMapper(this);
    connect(size_signal_mapper, SIGNAL(mapped(QString)), this, SLOT(applySizeChange(QString)));

        QRadioButton *radio_button;
        QGridLayout *grid_layout = new QGridLayout();
        qreal raw_no;
        raw_no = 0;
    
        int row_count = 4;
        QStringList size_list = properties->existingSizes();
        for(int i = 0; i < size_list.count(); ++i)
        {
            int row = i % row_count;
            int col = i / row_count;
    
            QString size = size_list.value(i);
            radio_button = new QRadioButton(size);
            size_signal_mapper->setMapping(radio_button, size);
            connect(radio_button, SIGNAL(clicked()), size_signal_mapper, SLOT(map()));
            grid_layout->addWidget(radio_button, row, col);
        }
    
        ui->size_groupBox->setLayout(grid_layout);@
    

    And It is working fine.

    After display the window I need to programatically check the one of them according to the state changes.

    How can I do this?

    Thanking you.



  • Hi,
    When your radioButton aren't members of your main class you could get the widget pointers via
    @
    QWidget * QWidget::childAt(int x, int y) const
    @
    on the ui->size_GroupBox.
    But then you need some form of column/row information about how many widgets there are. When the childAt return none zero, cast it to the radioButton and you could read out the state of the button.
    @
    if (ui->size_GroupBox.childAt(0,0) != NULL)
    {
    QRadioButton * RadioPointer = dynamic_cast<QRadioButton *>(ui->size_GroupBox.childAt(0,0));
    // RadioPointer may now be used as a pointer to the QRadioButton
    }
    @
    In my honest opinion it much easier to hold the radiobuttons in the ui pointer itself, so direct access is available from within your main class. When the radiobutton are dynamically placed, this should do the trick.

    Or use the QObject methode (if you set the name of the radiobuttons with setObjectName("")) via
    @
    findChild(RadioButtonName);
    @
    This also returns a QWidget pointer so the same handling could be done.



  • @ Jeroentje@home,

    Thank you very much for your reply.

    According to my case location based search is not possible. Location can be vary according to the size of _size_list _.

    I only know two things.

    1. text of the RadioButton
    2. It is inside the size_GroupBox

    Can I find the RadioButton using it's text?

    Thanks again.


  • Moderators

    [quote author="Hareen Laks" date="1383818030"]
    Can I find the RadioButton using it's text?
    [/quote]
    if it's unique of course.
    Use QButtonGroup::buttons() and iterate over them and compare their texts.



  • I solved the problem as below.

    @QList<QRadioButton *> allRadioButtons = ui->size_groupBox->findChildren<QRadioButton *>();

       foreach (QRadioButton *rb,allRadioButtons)
       {
           if(rb->text()== my_text)
           {
               rb->setChecked(true);
           }
       }@
    

    Thanks both of you.


Log in to reply
 

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