qt QGroupBox radiobutton
QGroupBox
显示文本:属性 title
加载radiobutton
if (jsonObject.contains("startEndTogether") && jsonObject["startEndTogether"].isString()) {QString selectedButton = jsonObject["startEndTogether"].toString();// Iterate through the children of groupBox_2 to find the QRadioButtonconst QObjectList &children = ui->groupBox_2->children();for (QObject *child : children) {QRadioButton *radioButton = qobject_cast<QRadioButton*>(child);if (radioButton) {// Check if the radio button's object name matches the value from jsonObjectif (radioButton->objectName() == "radioButton_3" && selectedButton == "1") {radioButton->setChecked(true);} else if (radioButton->objectName() == "radioButton_4" && selectedButton == "2") {radioButton->setChecked(true);}}}}
读取radiobutton保存json
jsonObject["testType"] = "考核";jsonObject["deviceNumber"] = "deviceNumber";jsonObject["turns"] = ui->turns->toPlainText();jsonObject["capacity"] = ui->capacity->toPlainText();jsonObject["minDuration"] = ui->minDuration->toPlainText();jsonObject["startEndTogether"] = "2";jsonObject["recodePosition"] = "1";jsonObject["userIds"] = "userIds";const QObjectList &children = ui->groupBox_2->children();for (QObject *child : children) {if (QRadioButton *radioButton = qobject_cast<QRadioButton*>(child)) {if (radioButton->isChecked()){if (radioButton->objectName()=="radioButton_3"){jsonObject["startEndTogether"] = "1";}}}// Add other widget types as needed}const QObjectList &children_2 = ui->groupBox->children();for (QObject *child : children_2) {if (QRadioButton *radioButton = qobject_cast<QRadioButton*>(child)) {if (radioButton->isChecked()){if (radioButton->objectName()=="startEndTogether_1"){jsonObject["recodePosition"] = "0";}}}// Add other widget types as needed}// Optionally, print JSON object for debuggingQJsonDocument doc(jsonObject);QString jsonString = doc.toJson(QJsonDocument::Compact);qDebug() << "Current JSON:" << jsonString;QFile saveFile(config_path);if (!saveFile.open(QIODevice::WriteOnly)) {qWarning("Couldn't open save file.");return;}QJsonDocument saveDoc(jsonObject);saveFile.write(saveDoc.toJson());saveFile.close();