Solved how to compress piece code repetitive?
-
Hi every body, I got this piece of code in few words runs which wanna compress better because has repetitive content.
for(int rowCount = 0; rowCount < ui->ctrldocTree->rowCount(); rowCount++){ QComboBox *comboBoxPlants = new QComboBox(this), *comboBoxPhysic = new QComboBox(this), *comboBoxSystem = new QComboBox(this), *comboBoxEquipment = new QComboBox(this); foreach(auto m, plantList) comboBoxPlants->insertItem(m["id"].toInt(), m["name"]); if(listElements[rowCount]["plant"] > 0){ comboBoxPlants->setCurrentText(listElements[rowCount]["plant"]); }else{ comboBoxPlants->setCurrentText(main_plant); } foreach(auto m, physicList) comboBoxPhysic->insertItem(m["id"].toInt(), m["name"]); if(listElements[rowCount]["area"] > 0){ comboBoxPhysic->setCurrentText(listElements[rowCount]["area"]); }else{ comboBoxPhysic->setCurrentText(main_plant); } foreach(auto m, systemList) comboBoxSystem->insertItem(m["id"].toInt(), m["name"]); comboBoxSystem->setCurrentText(main_system); if(listElements[rowCount]["system"] > 0){ comboBoxSystem->setCurrentText(listElements[rowCount]["system"]); }else{ comboBoxSystem->setCurrentText(main_plant); } foreach(auto m, equipmentList) comboBoxEquipment->insertItem(m["id"].toInt(), m["name"]); ui->ctrldocTree->setCellWidget(rowCount, CD_PLANT, comboBoxPlants); ui->ctrldocTree->setCellWidget(rowCount, CD_PHYSIC, comboBoxPhysic); ui->ctrldocTree->setCellWidget(rowCount, CD_SYSTEM, comboBoxSystem); ui->ctrldocTree->setCellWidget(rowCount, CD_EQUIPMENT, comboBoxEquipment); }
-
Hi
foreach(auto m, plantList) comboBoxPlants->insertItem(m["id"].toInt(), m["name"]); if(listElements[rowCount]["plant"] > 0){ comboBoxPlants->setCurrentText(listElements[rowCount]["plant"]); }else{ comboBoxPlants->setCurrentText(main_plant); } foreach(auto m, physicList) comboBoxPhysic->insertItem(m["id"].toInt(), m["name"]); if(listElements[rowCount]["area"] > 0){ comboBoxPhysic->setCurrentText(listElements[rowCount]["area"]); }else{ comboBoxPhysic->setCurrentText(main_plant); } foreach(auto m, systemList) comboBoxSystem->insertItem(m["id"].toInt(), m["name"]); comboBoxSystem->setCurrentText(main_system); if(listElements[rowCount]["system"] > 0){ comboBoxSystem->setCurrentText(listElements[rowCount]["system"]); }else{ comboBoxSystem->setCurrentText(main_plant); }
These you could use a function for.
something likevoid MainWindow::InsertInCombo(QComboxBox* combo,QString Key, int rowCount ) { foreach (auto m, List) combo->insertItem(m["id"].toInt(), m["name"]); if (listElements[rowCount][key] > 0) { combo->setCurrentText(listElements[rowCount][key]); } else { combo->setCurrentText(main_plant); } }
and call each like
InsertInCombo(comboBoxPhysic,physicList,"area",rowCount );Its just psudocode but i hope you get the idea.