my-QT专栏

QT 遍历控件和配置文件填充,修改了不用管逻辑

2021-09-27  本文已影响0人  c之气三段
单选框点选不是键值对模式,是一对多关系单独定义数据类型
class Radio
{
public:
    Radio(QString groupName,QString value)
    {
        this->groupName = groupName;
        this->value = value;
    }
    QString groupName;
    QString value;
};
    QMap<QString,Radio*> m_radioPara;
    QMap<QString,QString> m_lineEditPara;
    QMap<Radio*,QRadioButton*> m_radios;
    QMap<QString,QLineEdit*> m_lineEdits;
    QMap<QString,QString> m_jsonPara;
    m_radioPara =
    {
        {"spinms",new Radio("Source Type","Spinning Monopole Source")},
        {"spinds",new Radio("Source Type","Spinning Dipole Source")},
        {"staticms",new Radio("Source Type","Static Monopole Source")},
        {"staticds",new Radio("Source Type","Static Dipole Source")}
    };
    m_lineEditPara =
    {
        {"spinms","Spinning Monopole Source"},
        {"spinds","Spinning Dipole Source"},
        {"staticms","Static Monopole Source"},
        {"staticds","Static Dipole Source"},
        {"amplitude","Amplitude"},
        {"sourcecenter","Source Center"},
        {"radius","Radius"},
        {"frequency","Internal_Frequency"},
        {"force","Force"},
        {"dipolesource","Dipole Source"},
        {"ma","m - Azimuthal Mode"},
        {"nr","n1"},
        {"lineEdit_end","n_end"},
        {"frequency_2","External_Frequency"},
        {"lineEdit_frequStart","Frequency Start"},
        {"lineEdit_freStep","Frequency Step"}
    };
    //获取界面控件指针
    auto radios = this->findChildren<QRadioButton *>();
    for(auto radio:radios)
    {
        QString objname = radio->objectName();
        m_radios.insert(m_radioPara.value(objname),radio);
    }
    auto lineEdits = this->findChildren<QLineEdit*>();
    for (auto lineEdit:lineEdits)
    {
        QString objectName = lineEdit->objectName();
        m_lineEdits.insert(m_lineEditPara.value(objectName),lineEdit);
    }
QJsonParseError jsonerror;
    QJsonDocument json = QJsonDocument::fromJson(strJson.toStdString().c_str(), &jsonerror);
    if(jsonerror.error != QJsonParseError::NoError)
    {
        return;
    }
    m_jsonPara=JsonUtil::JsonToMapStringString(json.object());
    for (auto iter = m_radios.begin(); iter != m_radios.end(); ++iter)
    {
        QString para = m_jsonPara.value(iter.key()->groupName);
        if(para == iter.key()->value)
        {
            iter.value()->setChecked(true);
            break;
        }
    }
    for (auto iter = m_lineEdits.begin(); iter != m_lineEdits.end(); ++iter)
    {
        QString para = m_jsonPara.value(iter.key());
        iter.value()->setText(para);
    }
for (auto iter = m_radios.begin(); iter != m_radios.end(); ++iter)
    {
        if(iter.value()->isChecked())
        {
            m_jsonPara[iter.key()->groupName] = iter.key()->value;
            break;
        }
    }
    for (auto iter = m_lineEdits.begin(); iter != m_lineEdits.end(); ++iter)
    {
        m_jsonPara[iter.key()] = iter.value()->text();
    }

   QString json="{";
   for(auto it=m_jsonPara.begin();it!=m_jsonPara.end();it++)
   {
       QString key = it.key();
       QString value = it.value();
       json.append("\""+key+"\":\""+value+"\",");
   }
   json = json.remove(json.length()-1,1);
   json.append("}");

   return json;
上一篇 下一篇

猜你喜欢

热点阅读