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. updating radio buttons programmatically

updating radio buttons programmatically

Scheduled Pinned Locked Moved Solved General and Desktop
44 Posts 4 Posters 11.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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    A couple months ago, I implemented a model/view based app. I need to add a pair of radio buttons to the widget. These buttons must be under programmatic control; that is, my worker object needs to be able to change the selected button without user intervention.

    I have a row in my model representing the desired selection of the buttons. What I'm not clear on is how to map this column in the model to the UI. Can someone provide some guidance?

    Thanks...

    Pablo J. RoginaP 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you explain the relation between you worker, model and UI ?

      It’s not very clear when your button should be modified.

      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
      • mzimmersM mzimmers

        Hi all -

        A couple months ago, I implemented a model/view based app. I need to add a pair of radio buttons to the widget. These buttons must be under programmatic control; that is, my worker object needs to be able to change the selected button without user intervention.

        I have a row in my model representing the desired selection of the buttons. What I'm not clear on is how to map this column in the model to the UI. Can someone provide some guidance?

        Thanks...

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @mzimmers a screenshot of your current UI may help a lot as well.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          Hey guys -

          Here's what I'm trying to do:

          0_1534362400213_dialog.PNG

          This dialog enables a user to review and change configuration details. (There's a "save" and a "cancel" button as well.) But, I need to pre-populate this dialog with the current configuration details (like which IP assignment method is in use).

          My target device passes an XML message to my (Qt based) host. A worker thread receives and parses the message, and updates the model via a signal. For all my text-based widgets, I use a mapper call:

              m_mapper->addMapping(ui->ipAddress, TAG_IPV4ADDRESS);
          

          But I don't know how to do this for a radio button.

          Pablo J. RoginaP 1 Reply Last reply
          0
          • mzimmersM mzimmers

            Hey guys -

            Here's what I'm trying to do:

            0_1534362400213_dialog.PNG

            This dialog enables a user to review and change configuration details. (There's a "save" and a "cancel" button as well.) But, I need to pre-populate this dialog with the current configuration details (like which IP assignment method is in use).

            My target device passes an XML message to my (Qt based) host. A worker thread receives and parses the message, and updates the model via a signal. For all my text-based widgets, I use a mapper call:

                m_mapper->addMapping(ui->ipAddress, TAG_IPV4ADDRESS);
            

            But I don't know how to do this for a radio button.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @mzimmers assuming that you have a QButtonGroup for the "IP Address Assignment" I guess you may want to take a look at this capability from such widget:

            In addition, QButtonGroup can map between integers and buttons. You can assign an integer id to a button with setId(), and retrieve it with id(). The id of the currently checked button is available with checkedId(), and there is an overloaded signal buttonClicked() which emits the id of the button. The id -1 is reserved by QButtonGroup to mean "no such button". The purpose of the mapping mechanism is to simplify the representation of enum values in a user interface.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

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

              What you can do is create a custom widget for your address type widget and add a property to it and in the setter you activate the correct button based on the text passed.

              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
              0
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                OK, this is helpful. I've added the following to my model update routine:

                    // the IP configuration source.
                    s = msg->getValue(msgTag[TAG_IP_CONFIG_SRC]);
                    if (s == IP_SOURCE_TXT[IP_SOURCE_DHCP])
                    {
                        m_model->setData(m_model->index(row, TAG_IP_CONFIG_SRC), IP_SOURCE_DHCP);
                    }
                    else
                    {
                        m_model->setData(m_model->index(row, TAG_IP_CONFIG_SRC), IP_SOURCE_STATIC);
                    }
                

                And the following in the c'tor of my dialog:

                   QButtonGroup *m_qbg;    // button group for IP source selection.
                     ...
                    m_qbg->addButton(ui->ipDhcp);
                    m_qbg->setId(ui->ipDhcp, IP_SOURCE_DHCP);
                    m_qbg->addButton(ui->ipStatic);
                    m_qbg->setId(ui->ipStatic, IP_SOURCE_STATIC);
                

                Do I need to read the model data, and manually change the appropriate button's checked status? I can do this, but...I don't know where I'd put that code. A slot to accept the buttonChecked() signal would work for user-initiated changes, but I don't think that will work for programmatic changes, will it?

                Pablo J. RoginaP 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  OK, this is helpful. I've added the following to my model update routine:

                      // the IP configuration source.
                      s = msg->getValue(msgTag[TAG_IP_CONFIG_SRC]);
                      if (s == IP_SOURCE_TXT[IP_SOURCE_DHCP])
                      {
                          m_model->setData(m_model->index(row, TAG_IP_CONFIG_SRC), IP_SOURCE_DHCP);
                      }
                      else
                      {
                          m_model->setData(m_model->index(row, TAG_IP_CONFIG_SRC), IP_SOURCE_STATIC);
                      }
                  

                  And the following in the c'tor of my dialog:

                     QButtonGroup *m_qbg;    // button group for IP source selection.
                       ...
                      m_qbg->addButton(ui->ipDhcp);
                      m_qbg->setId(ui->ipDhcp, IP_SOURCE_DHCP);
                      m_qbg->addButton(ui->ipStatic);
                      m_qbg->setId(ui->ipStatic, IP_SOURCE_STATIC);
                  

                  Do I need to read the model data, and manually change the appropriate button's checked status? I can do this, but...I don't know where I'd put that code. A slot to accept the buttonChecked() signal would work for user-initiated changes, but I don't think that will work for programmatic changes, will it?

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @mzimmers said in updating radio buttons programmatically:

                  but I don't think that will work for programmatic changes,

                  Where are you parsing the XML data sent by the device(s)?

                  I guess that wherever you are setting the IP address/netmask/etc you can set the IP mode, i.e.

                  foreach (QAbstractButton* button, n_qbg->buttons())
                  {
                      if (id(button) == <ID_VALUE_FROM_XML_EITHER_DHCP-ID_OR_STATIC-ID>) {
                          button->setChecked(true);
                          break;
                      }
                  }
                  

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  mzimmersM 1 Reply Last reply
                  0
                  • Pablo J. RoginaP Pablo J. Rogina

                    @mzimmers said in updating radio buttons programmatically:

                    but I don't think that will work for programmatic changes,

                    Where are you parsing the XML data sent by the device(s)?

                    I guess that wherever you are setting the IP address/netmask/etc you can set the IP mode, i.e.

                    foreach (QAbstractButton* button, n_qbg->buttons())
                    {
                        if (id(button) == <ID_VALUE_FROM_XML_EITHER_DHCP-ID_OR_STATIC-ID>) {
                            button->setChecked(true);
                            break;
                        }
                    }
                    
                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #9

                    @Pablo-J.-Rogina The worker receives the XML message and passes it to the object that contains the model. For each field, I have a bit of code like this:

                        // the IP address.
                        qs = QString::fromStdString(msg->getValue(msgTag[TAG_IPV4ADDRESS]));
                        m_model->setData(m_model->index(row, TAG_IPV4ADDRESS), qs);
                    

                    I can set model data from here, but I don't have direct access to dialog widgets.

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @Pablo-J.-Rogina The worker receives the XML message and passes it to the object that contains the model. For each field, I have a bit of code like this:

                          // the IP address.
                          qs = QString::fromStdString(msg->getValue(msgTag[TAG_IPV4ADDRESS]));
                          m_model->setData(m_model->index(row, TAG_IPV4ADDRESS), qs);
                      

                      I can set model data from here, but I don't have direct access to dialog widgets.

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @mzimmers said in updating radio buttons programmatically:

                      but I don't have direct access to dialog widgets.

                      But at some point data from the model is used to update the dialog widgets?
                      If not, how the IP address value is taken from?
                      If so, what about adding the IP address mode (DHCP/Static) field to your existing model?

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by mzimmers
                        #11

                        I use a QDataWidgetMapper object in my dialog to access the fields.

                            m_mapper->addMapping(ui->ipAddress, TAG_IPV4ADDRESS);
                        

                        This maps a line edit to a column in my model. But I don't think I can simply do the same thing for a button group.

                        EDIT: by the way, I don't have to use radio buttons for this. They seemed like a good choice, but if there's a better method, I'm happy to entertain ideas.

                        Pablo J. RoginaP 1 Reply Last reply
                        0
                        • mzimmersM mzimmers

                          I use a QDataWidgetMapper object in my dialog to access the fields.

                              m_mapper->addMapping(ui->ipAddress, TAG_IPV4ADDRESS);
                          

                          This maps a line edit to a column in my model. But I don't think I can simply do the same thing for a button group.

                          EDIT: by the way, I don't have to use radio buttons for this. They seemed like a good choice, but if there's a better method, I'm happy to entertain ideas.

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by Pablo J. Rogina
                          #12

                          @mzimmers said in updating radio buttons programmatically:

                          I use a QDataWidgetMapper object in my dialog

                          having stated this from the very beginning should have saved time and effort, and allow us to narrow down the issue... :-)

                          Given that said, maybe this post may help

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          mzimmersM 1 Reply Last reply
                          0
                          • Pablo J. RoginaP Pablo J. Rogina

                            @mzimmers said in updating radio buttons programmatically:

                            I use a QDataWidgetMapper object in my dialog

                            having stated this from the very beginning should have saved time and effort, and allow us to narrow down the issue... :-)

                            Given that said, maybe this post may help

                            mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by
                            #13

                            @Pablo-J.-Rogina said in updating radio buttons programmatically:

                            @mzimmers said in updating radio buttons programmatically:

                            I use a QDataWidgetMapper object in my dialog

                            having stated this from the very beginning should have saved time and effort, and allow us to narrow down the issue... :-)

                            Sorry -- I thought I had done so here:
                            https://forum.qt.io/topic/93723/updating-radio-buttons-programmatically/4

                            In any event, it may be time to consider a different approach.

                            SGaist: looking at your response, I have a couple questions:

                            1. you mention customizing my address type widget - are you referring to the buttongroup?
                            2. What do you mean by "the setter"?
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              I mean make a custom widget e.g.:

                              class IpAddressTypeWidget : public Widget
                              {
                                  Q_OBJECT
                                  Q_PROPERTY(QString addressType GET addressType SET setAddressType NOTIFY addressTypeChanged USER true)
                              
                              public:
                                  explicit IpAddressTypeWidget(QWidget *parent=nullptr);
                              
                                  QString addressType() const;
                              
                              signals:
                                  void addressTypeChanged(const QString& type);
                              
                              public slots:
                                  void setAddressType(const QString& type);
                              };
                              

                              In setAddressType, you update the radio buttons based on the string given. Note that you can also use an enum and it's the role of the parser to use the correct enum when changing the address type. It would be cleaner but for a first round, it might be easier to use a QString.

                              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
                              • mzimmersM Offline
                                mzimmersM Offline
                                mzimmers
                                wrote on last edited by
                                #15

                                OK, I think I understand what you're suggesting, but some of the mechanics still aren't clear to me.

                                Does this new widget replace any of my existing widgets, or does it just exist (invisibly) within my dialog?

                                Do I need to pass it my ui pointer at construction to give it access to the rest of the widgets?

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

                                  It's an independent widget where you put your QGroupBox which contains the two QRadioButton.

                                  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
                                  0
                                  • mzimmersM Offline
                                    mzimmersM Offline
                                    mzimmers
                                    wrote on last edited by
                                    #17

                                    I'm still wading through this, but I did discover a necessary change in the Q_PROPERTY macro:

                                        Q_PROPERTY(QString ipSource READ ipSource WRITE SetIpSource NOTIFY ipSourceChanged USER true)
                                    

                                    It wouldn't compile with the GET/SET terms.

                                    1 Reply Last reply
                                    0
                                    • mzimmersM Offline
                                      mzimmersM Offline
                                      mzimmers
                                      wrote on last edited by
                                      #18

                                      SGaist: in your example above, where does the signal come from that connects to the slot setAddressType()?

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

                                        IIRC, you are using a QDataWidgetMapper for that editor ? Correct, then with that definition, when you call addMapping it will use that property to get and set the data from that widget in the same manner as it does for QLineEdit or QSpinBox.

                                        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
                                        0
                                        • mzimmersM Offline
                                          mzimmersM Offline
                                          mzimmers
                                          wrote on last edited by
                                          #20

                                          OK, I think I get this in principle. From an implementation standpoint, how/when should the edit dialog retrieve this information from the custom widget -- another signal/slot?

                                          Thanks...

                                          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