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. [Solved] Programatically check the RadioButton inside a GroupBox
Forum Updated to NodeBB v4.3 + New Features

[Solved] Programatically check the RadioButton inside a GroupBox

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.5k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        @ 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.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hareen Laks
            wrote on last edited by
            #5

            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.

            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