解决的问题:
平常要存储一些临时数据,或者ini的系统参数数据,以下是源码解析
如何实现:
实现的UI如下
主要功能:
初始化:
m_settings = new QSettings("DParamSetting.ini", QSettings::IniFormat);
设置值
m_settings->beginGroup(QString("%1").arg(groupName));
m_settings->setValue("t1Value", dParam.t1Value);
m_settings->endGroup();
取值
dParam.t1Value = m_settings->value(QString("%1/%2").arg(groupName).arg("t1Value")).toInt();
代码块解析:
.h文件
#ifndef DPARAMSETTING_H
#define DPARAMSETTING_H#include <QWidget>
#include <QComboBox>
#include <QPushButton>
#include <QLineEdit>
#include <QSettings>typedef struct
{int t1Value;int t2Value;int t3Value;int frequency1Value;int frequency2Value;int amplitude1Value;int amplitude2Value;int numDropValue;
}DropletParam;class DParamSetting : public QWidget
{Q_OBJECTpublic:DParamSetting(QWidget *parent = 0);~DParamSetting();private:void initObject();void initGeometry();void initConnect();void readDParams(const QString groupName, DropletParam &dParam);void writeDParams(const QString groupName, const DropletParam dParam);DropletParam assemblingDatagram();void displayDatagram(const DropletParam dParam);private slots:void onSaveBtnClicked();void onCancelBtnClicked();void onStartBtnClicked();void onSourBoardChaned(const QString &str);private:QComboBox *m_pSourBoardCb; QPushButton *m_pT1Btn;QPushButton *m_pT2Btn;QPushButton *m_pT3Btn;QPushButton *m_pFr1Btn;QPushButton *m_pFr2Btn;QPushButton *m_pAmp1Btn;QPushButton *m_pAmp2Btn;QPushButton *m_pNumberBtn;QLineEdit *m_pT1Edit;QLineEdit *m_pT2Edit;QLineEdit *m_pT3Edit;QLineEdit *m_pFr1Edit;QLineEdit *m_pFr2Edit;QLineEdit *m_pAmp1Edit;QLineEdit *m_pAmp2Edit;QLineEdit *m_pNumberEdit;QPushButton *m_pSaveBtn;QPushButton *m_pStartBtn;QPushButton *m_pCancelBtn;QSettings *m_settings;
};#endif // DPARAMSETTING_H
.cpp文件
#include "dparamsetting.h"
#include <QGridLayout>
#include <QDebug>DParamSetting::DParamSetting(QWidget *parent): QWidget(parent)
{initObject();initGeometry();initConnect();
}DParamSetting::~DParamSetting()
{delete m_settings;
}void DParamSetting::initObject()
{m_pSourBoardCb = new QComboBox(this); QStringList strlist;strlist.append("abc");strlist.append("aba");m_pSourBoardCb->addItems(strlist);m_pT1Btn = new QPushButton("T1", this);m_pT2Btn = new QPushButton("T2", this);m_pT3Btn = new QPushButton("T3", this);m_pFr1Btn = new QPushButton("Fre1", this);m_pFr2Btn = new QPushButton("Fre2", this);m_pAmp1Btn = new QPushButton("Amp1", this);m_pAmp2Btn = new QPushButton("Amp2", this);m_pNumberBtn = new QPushButton("Number", this);m_pT1Edit = new QLineEdit("0", this);m_pT2Edit = new QLineEdit("0", this);m_pT3Edit = new QLineEdit("0", this);m_pFr1Edit = new QLineEdit("0", this);m_pFr2Edit = new QLineEdit("0", this);m_pAmp1Edit = new QLineEdit("0", this);m_pAmp2Edit = new QLineEdit("0", this);m_pNumberEdit = new QLineEdit("0", this);m_pSaveBtn = new QPushButton("Save", this);m_pStartBtn = new QPushButton("Start", this);m_pCancelBtn = new QPushButton("Cancel", this);m_settings = new QSettings("DParamSetting.ini", QSettings::IniFormat);}void DParamSetting::initGeometry()
{QGridLayout *glayout = new QGridLayout(this);glayout->addWidget(m_pSourBoardCb, 1, 1, 1, 2, Qt::AlignLeft);glayout->addWidget(m_pT1Btn, 2, 1, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pT1Edit, 2, 2, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pFr1Btn, 2, 3, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pFr1Edit, 2, 4, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pAmp1Btn, 2, 5, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pAmp1Edit, 2, 6, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pT2Btn, 3, 1, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pT2Edit, 3, 2, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pFr2Btn, 3, 3, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pFr2Edit, 3, 4, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pAmp2Btn, 3, 5, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pAmp2Edit, 3, 6, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pT3Btn, 4, 1, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pT3Edit, 4, 2, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pNumberBtn, 4, 3, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pNumberEdit, 4, 4, 1, 1, Qt::AlignCenter);glayout->addWidget(m_pStartBtn, 5, 4, 1, 1, Qt::AlignRight);glayout->addWidget(m_pSaveBtn, 5, 5, 1, 1, Qt::AlignRight);glayout->addWidget(m_pCancelBtn, 5, 6, 1, 1, Qt::AlignLeft);this->setLayout(glayout);
}void DParamSetting::initConnect()
{connect(m_pSaveBtn, &QPushButton::clicked, this, &DParamSetting::onSaveBtnClicked);connect(m_pCancelBtn, &QPushButton::clicked, this, &DParamSetting::onCancelBtnClicked);connect(m_pSourBoardCb, &QComboBox::currentTextChanged, this, &DParamSetting::onSourBoardChaned);
}void DParamSetting::readDParams(const QString groupName, DropletParam &dParam)
{dParam.t1Value = m_settings->value(QString("%1/%2").arg(groupName).arg("t1Value")).toInt();dParam.t2Value = m_settings->value(QString("%1/%2").arg(groupName).arg("t2Value")).toInt();dParam.t3Value = m_settings->value(QString("%1/%2").arg(groupName).arg("t3Value")).toInt();dParam.frequency1Value = m_settings->value(QString("%1/%2").arg(groupName).arg("frequency1Value")).toInt();dParam.frequency2Value = m_settings->value(QString("%1/%2").arg(groupName).arg("frequency2Value")).toInt();dParam.amplitude1Value = m_settings->value(QString("%1/%2").arg(groupName).arg("amplitude1Value")).toInt();dParam.amplitude2Value = m_settings->value(QString("%1/%2").arg(groupName).arg("amplitude2Value")).toInt();dParam.numDropValue = m_settings->value(QString("%1/%2").arg(groupName).arg("numDropValue")).toInt();
}void DParamSetting::writeDParams(const QString groupName, const DropletParam dParam)
{m_settings->beginGroup(QString("%1").arg(groupName));m_settings->setValue("t1Value", dParam.t1Value);m_settings->setValue("t2Value", dParam.t2Value);m_settings->setValue("t3Value", dParam.t3Value);m_settings->setValue("frequency1Value", dParam.frequency1Value);m_settings->setValue("frequency2Value", dParam.frequency2Value);m_settings->setValue("amplitude1Value", dParam.amplitude1Value);m_settings->setValue("amplitude2Value", dParam.amplitude2Value);m_settings->setValue("numDropValue", dParam.numDropValue);m_settings->endGroup();
}DropletParam DParamSetting::assemblingDatagram()
{DropletParam ts;ts.t1Value = m_pT1Edit->text().toInt();ts.t2Value = m_pT2Edit->text().toInt();ts.t3Value = m_pT3Edit->text().toInt();ts.frequency1Value = m_pFr1Edit->text().toInt();ts.frequency2Value = m_pFr2Edit->text().toInt();ts.amplitude1Value = m_pAmp1Edit->text().toInt();ts.amplitude2Value = m_pAmp2Edit->text().toInt();ts.numDropValue = m_pNumberEdit->text().toInt();return ts;}void DParamSetting::displayDatagram(const DropletParam dParam)
{m_pT1Edit->setText(QString::number(dParam.t1Value));m_pT2Edit->setText(QString::number(dParam.t2Value));m_pT3Edit->setText(QString::number(dParam.t3Value));m_pFr1Edit->setText(QString::number(dParam.frequency1Value));m_pFr2Edit->setText(QString::number(dParam.frequency2Value));m_pAmp1Edit->setText(QString::number(dParam.amplitude1Value));m_pAmp2Edit->setText(QString::number(dParam.amplitude2Value));m_pNumberEdit->setText(QString::number(dParam.numDropValue));
}void DParamSetting::onSaveBtnClicked()
{writeDParams(m_pSourBoardCb->currentText(), assemblingDatagram());
}void DParamSetting::onCancelBtnClicked()
{DropletParam ts;readDParams(m_pSourBoardCb->currentText(), ts);displayDatagram(ts);
}void DParamSetting::onStartBtnClicked()
{}void DParamSetting::onSourBoardChaned(const QString &str)
{DropletParam ts;readDParams(str, ts);displayDatagram(ts);
}