updating radio buttons programmatically
-
@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; } }
-
@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.
-
@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? -
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.
-
@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
-
@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/4In any event, it may be time to consider a different approach.
SGaist: looking at your response, I have a couple questions:
- you mention customizing my address type widget - are you referring to the buttongroup?
- What do you mean by "the setter"?
-
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. -
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?
-
It's an independent widget where you put your
QGroupBox
which contains the twoQRadioButton
. -
IIRC, you are using a
QDataWidgetMapper
for that editor ? Correct, then with that definition, when you calladdMapping
it will use that property to get and set the data from that widget in the same manner as it does forQLineEdit
orQSpinBox
. -
I'm not sure I'm following you here. Isn't that widget already using
QDataWidgetMapper
? -
Yes:
in editdialog.cpp
IpSourceWidget m_isw; // IpSourceWidget is my custom widget m_mapper->addMapping(&m_isw, TAG_IP_CONFIG_SRC);
But don't I still have to use this to update the group box? Like this:
if (qs.toStdString() == IP_SOURCE_TXT[IP_SOURCE_DHCP]) { ui->groupBoxIpAssignment->setChecked(IP_SOURCE_DHCP); } else { ui->groupBoxIpAssignment->setChecked(IP_SOURCE_STATIC); }
EDIT: OK, that wasn't right. I changed it to this:
QButtonGroup *m_qbg; // button group for IP source selection. ... if (qs.toStdString() == IP_SOURCE_TXT[IP_SOURCE_DHCP]) { m_qbg->button(IP_SOURCE_DHCP)->setChecked(true); } else { m_qbg->button(IP_SOURCE_STATIC)->setChecked(true); }
This at least gets the correct value displaying in the edit dialog. Still not sure I'm really doing this right, though.
-
Don't forget to emit the corresponding notification signal if the address type has changed.
-
Not sure I understand. The code above is in a slot in my edit dialog. Elsewhere I have a connect:
IpSourceWidget *m_isw; ... QObject::connect(m_isw, &IpSourceWidget::ipSourceChanged, this, &EditDialog::updateIpSource);
Is this the signal you're referring to?
-
I was thinking about where you are emitting
ipSourceChanged
. -
Well, maybe that's part of the problem...I wasn't aware I needed to use that signal.
I'm still feeling my way through this. My IpSource widget doesn't contain any display items; it just handles the model updating. It communicates with the edit dialog through some shared variables:
IpSourceWidget::IpSourceWidget(QWidget *parent, DeviceModel *d, QModelIndex *qmi) : QWidget(parent), m_d(d), m_qmi(qmi) { } IpSourceWidget::~IpSourceWidget() { } QString IpSourceWidget::ipSource() const { return QString::fromStdString(IP_SOURCE_TXT[m_ics]); } void IpSourceWidget::setIpSource(const QString &source) { m_d->getModel()->setData(*m_qmi, source); }
I'm getting unexpected results, though. For example, after I press my "commit" button, the argument passed into setIpSource is set to "false." No idea where that's coming from.
-
Why does IpSourceWidget know about DeviceModel and the corresponding QModelIndex ?
The idea behind that widget is that you use it the same way as the QLineEdit you have in your editor widget.
For example in a widget like QLineEdit you have something like:
void MyWidget::setMyStringProperty(const QString &newValue) { if (newValue == currentValue) { return; } currentValue = newValue; // Do stuff if needed emit myStringPropertyValueChanged(newValue); }