Important: Please read the Qt Code of Conduct -

updating radio buttons programmatically

  • @mzimmers Check that "ui_ipsource.h" contains what you expect.

  • Ah...that revealed the problem. In my fumbling around, I'd renamed the class in Designer. I just changed it back, and it seems to be good now. back to my struggles with those radio buttons.

  • I've added a Widget to my edit dialog, and promoted it to class IpSource. It displays correctly, but I can't access it programmatically, at least not with the "ui->" pointer I'm accustomed to using.

    Did I miss a step? I've never used promotions before.

    EDIT: this question is irrelevant to the thread, so I'm going to ask it in a separate thread.

  • OK, I believe I now have this beaten into submission. Here's my understanding of the necessary ingredients:

    1. upon determining the need for an internal (programmatic) change to the status of the radio buttons, the model needs to be set or updated appropriately:
        // the IP configuration source.
        QString qs = QString::fromStdString(msg->getValue(msgTag[TAG_IP_CONFIG_SRC]));
        QVariant qv = m_model->data(m_model->index(row, TAG_IP_CONFIG_SRC));
        if (qs != qv)
            m_model->setData(m_model->index(row, TAG_IP_CONFIG_SRC), qs);
            emit ipSourceChanged(qs);
    1. a custom designer class, based on QGroupBox, is necessary:
    class IpSource : public QGroupBox
        Q_PROPERTY(QString ipSource READ ipSource WRITE setIpSource NOTIFY ipSourceChanged USER true)
        explicit IpSource(QWidget *parent = nullptr);
        QString ipSource() const;
        void ipSourceChanged(const QString &source);
    public slots:
        void setIpSource(const QString &source);
        Ui::IpSource *ui;
    1. from your parent dialog (if that's the right term), create an empty widget, and promote it to your custom widget.
    2. the syntax for retrieving the value of the radio button group was a little tricky at first, but it goes like this:
            string s = ui->ipSource->ipSource().toStdString();

    (you can ignore the StdString part if you're just using QStrings; I have to share my constants header file with a firmware app.

    If anyone feels that the above needs correction/improvement, please let me know.

    Thanks to everyone who helped on this.

Log in to reply