提要
当程序关闭后再次打开程序需要加载上一次的按钮名称设置。
思路
关闭程序的时候保存此刻界面按钮的文本,正常情况下只需在程序退出时调用的析构函数的内部实现,将界面所有的按钮文本生成一个文件,在程序下一次启动的时候,读取文件,设置按钮文本。这里需要考虑的是生成的文件的格式,读取文件时保存文件中的按钮文本的数据结构,当然由于按钮的顺序对应着相应的文本,这里采用每个按钮都有相应的id和index来标识,以找到对应的文本。
数据解构
其中index表示是第几个按钮,id表示对应文本的按钮id,name为按钮要显示的文本。
{"btnInfo": [{"id": 10008,"index": 1,"name": "场景8"}, {"id": 10026,"index": 2,"name": "场景K-P蝴蝶飞"}, {"id": 10014,"index": 3,"name": "场景即"}, {"id": 10021,"index": 6,"name": "场景及对QP"}, {"id": 10021,"index": 7,"name": "场景及对QP"}]
}
上面json文件的结构中,第一个按钮的文本为场景8,按钮的id为10008,依次类推。
也就是关闭程序的时候,需在析构函数中生成该格式的json文件。
SystemScenenForm::~SystemScenenForm()
{createJsonFile();//生成json文件delete ui;
}
生成json文件,其中变量m_btnInfoMap是用来保存界面按钮的文本的容器,QMap<int,stuSceNameId> m_btnInfoMap;//保存按钮的文本,id,按钮下标,以按钮下标为键
struct stuSceNameId
{QString name;int id;
};
//以上结构体仅作为类型说明void SystemScenenForm::createJsonFile()
{QJsonObject data;QJsonObject subData1;QJsonArray array;int index;QMap<int,stuSceNameId>::iterator it;for(it = m_btnInfoMap.begin(); it != m_btnInfoMap.end(); ++it){index = it.key();stuSceNameId temp = *it;subData1.insert("index",index);subData1.insert("id",temp.id);subData1.insert("name",QString::fromLocal8Bit(temp.name.toLocal8Bit()));//将utf-8转换为gbkarray.append(subData1);}data.insert("btnInfo",array);QJsonDocument document;document.setObject(data);QByteArray jsonArray = document.toJson(QJsonDocument::Compact);QString strJson(jsonArray);QString strName = QCoreApplication::applicationDirPath() + "/sceneBtnInfo.json";QFile file(strName);if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){QString strTitle = "提示";QString strContent = QString("%1文件打开失败!").arg(strName);QMessageBox::information(this,strTitle,strContent);return ;}QTextStream in(&file);//生成的json文件中编码为gbkin<<strJson;file.close();
}
启动程序时读取json文件解析json,保存到内存中,设置按钮文本。
void SystemScenenForm::initSceneBtnInfo()
{QString strName = QCoreApplication::applicationDirPath() + "/sceneBtnInfo.json";QString byteArray = readSceneBtnInfo(strName);outPut<<"读到的字符串:"<<byteArray;if(byteArray.size() == 0){return ;}QByteArray array = byteArray.toUtf8();//将utf-8编码的QString类型变量转换为utf-8的QByteArray类型变量parseSceneBtnJson(array);//将m_scenBtnMap的id和name保存为从配置文件中读取的stuSceNameId temp;QMap<int,stuSceNameId>::iterator it;for(it = m_btnInfoMap.begin(); it != m_btnInfoMap.end(); ++it){temp = *it;m_scenBtnMap.insert(temp.id,temp.name);}
}//读json文件
QString SystemScenenForm::readSceneBtnInfo(QString fileName)
{QByteArray allData;
#ifdef OUT_PUToutPut<<"文件路径及名称:"<<fileName;
#endifQTextCodec *code = QTextCodec::codecForName("GBK");QFile file(fileName);if (!file.open(QIODevice::ReadOnly)){
#ifdef OUT_PUToutPut<<QString("%1配置文件读取失败!").arg(fileName);
#endifreturn allData;}allData = file.readAll();QString str = code->toUnicode(allData);//gbk编码的QByteArray类型变量转换为utf-8编码的QString变量file.close();
#ifdef OUT_PUT
// outPut<<"json文件内容:"<<allData;
#endifreturn str;
}//解析json
void SystemScenenForm::parseSceneBtnJson(QByteArray &byteArray)
{int tempIndex;stuSceNameId tempStu;if(m_btnInfoMap.size() != 0){m_btnInfoMap.clear();}QJsonObject jsonObject;QJsonParseError jsonError;QJsonDocument document = QJsonDocument::fromJson(byteArray,&jsonError);if(!document.isEmpty() && jsonError.error == QJsonParseError::NoError){QJsonObject resultObject = document.object();QJsonValue jsonValue = resultObject.value("btnInfo");if(jsonValue.isArray()){QJsonArray jsonArray = jsonValue.toArray();for(int i = 0; i < jsonArray.size(); ++i){jsonObject = jsonArray.at(i).toObject();tempIndex = jsonObject.value("index").toInt();tempStu.id = jsonObject.value("id").toInt();tempStu.name = jsonObject.value("name").toString();m_btnInfoMap.insert(tempIndex,tempStu);}}}else{outPut<<"sceneBtnInfo.json文件解析失败"<<jsonError.errorString();}
}//创建按钮
void SystemScenenForm::createSceneBtn()
{int nRow = 0;//判断配置文件是否有按钮信息if(m_btnInfoMap.size() != 0){myButton *btn = NULL;for(int i = 0; i < SCENEBTN_NUM; ++i){btn = new myButton(i+1,ui->backgroundwidget);//查找下标对应的按钮名称QMap<int,stuSceNameId>::iterator it = m_btnInfoMap.find(i+1);if(it != m_btnInfoMap.end()){stuSceNameId temp = *it;btn->setText(temp.name);btn->setCreateSceneBtnName(temp.name);}if(i != 0 && i % 10 == 0){nRow++;}btn->setGeometry((21 + (i%10)*(69+122)) / m_percentW,(13 + nRow*59) / m_percentH,122 / m_percentW,46 / m_percentH);btn->show();m_idBtnMap.insert(i+1,btn);connect(btn,&myButton::clicked,this,&SystemScenenForm::slot_scenTransfer);connect(btn,&myButton::signalSaveBtnInfo,this,&SystemScenenForm::slot_saveBtnInfo);connect(this,&SystemScenenForm::signalUpdateBtnNameList,btn,&myButton::slot_updateBtnList);}}
}
其中myButton为自定义的按钮类,基类为QPushButton, btn = new myButton(i+1,ui->backgroundwidget);参数i+1为index,即按钮的个数,从1开始,ui->backgroundwidget为父控件,SCENEBTN_NUM为宏定义,一个指定的数字,m_percentW和m_percentH为自适应分辨率时的宽之比和高之比。
这篇文章是之前写的文件,内容可能比乱。