for (int i=0; i<dataArray.count(); i++) { T_departmentInfo departmentInfo; departmentInfo.label.clear();// = dataArray.at(i).toObject().value("label").toString();; departmentInfo.departmentId = dataArray.at(i).toObject().value("departmentId").toString(); departmentInfo.departmentName = dataArray.at(i).toObject().value("departmentName").toString(); departmentInfo.departmentType = dataArray.at(i).toObject().value("departmentType").toInt();; //部门类型 0普通 1独立 departmentInfo.parentId.clear();// = dataArray.at(i).toObject().value("parentId").toString(); departmentInfo.phone.clear();// = dataArray.at(i).toObject().value("phone").toString(); departmentInfo.userId.clear();// = dataArray.at(i).toObject().value("userId").toString(); //空为部门,非空为员工 departmentInfo.userName.clear();// = dataArray.at(i).toObject().value("userName").toString(); departmentInfo.picture.clear();// = dataArray.at(i).toObject().value("picture").toString(); QStandardItem* item = new QStandardItem(departmentPersonModel->rowCount()); departmentPersonModel->appendRow(item); QModelIndex index = departmentPersonModel->indexFromItem(item); PersonItem *personItem = new PersonItem(ui->listView_department_person); personItem->setDepartmentInfo(departmentInfo); ui->listView_department_person->setIndexWidget(index, personItem);
时间: 2024-03-13 16:43:52 浏览: 104
对于这段代码,我们可以进行如下几个方面的优化:
1. 减少重复调用
在这段代码中,dataArray.at(i).toObject() 出现了多次,这样会增加不必要的计算。我们可以将其提取出来,赋值给一个对象,然后多次使用这个对象,从而减少计算量。
2. 优化数据类型
在这段代码中,对于一些字符串类型的变量,如parentId、phone、userId、userName、picture等,都是用clear()方法赋空值,这种方式比较耗费资源。我们可以将这些变量改为QString类型,并且在定义时就赋值为空字符串"",这样就不需要调用clear()方法了。
3. 减少创建对象
在这段代码中,每次循环都会创建一个QStandardItem和一个PersonItem对象,这样会占用大量的内存资源。我们可以将这些对象的创建放到循环外面,并且使用一个指针数组保存这些对象的指针,在循环中重复利用这些对象,从而减少内存的占用。
下面是优化后的代码:
```c++
QJsonObject obj;
for (int i = 0; i < dataArray.count(); i++) {
obj = dataArray.at(i).toObject();
T_departmentInfo departmentInfo;
departmentInfo.departmentId = obj.value("departmentId").toString();
departmentInfo.departmentName = obj.value("departmentName").toString();
departmentInfo.departmentType = obj.value("departmentType").toInt(); //部门类型 0普通 1独立
departmentInfo.parentId = obj.value("parentId").toString();
departmentInfo.phone = obj.value("phone").toString();
departmentInfo.userId = obj.value("userId").toString(); //空为部门,非空为员工
departmentInfo.userName = obj.value("userName").toString();
departmentInfo.picture = obj.value("picture").toString();
PersonItem *personItem = nullptr;
QStandardItem *item = nullptr;
if (i < itemArray.size()) {
item = itemArray.at(i);
personItem = static_cast<PersonItem *>(ui->listView_department_person->indexWidget(departmentPersonModel->indexFromItem(item)));
} else {
item = new QStandardItem(departmentPersonModel->rowCount());
departmentPersonModel->appendRow(item);
personItem = new PersonItem(ui->listView_department_person);
itemArray.append(item);
widgetArray.append(personItem);
}
item->setData(departmentInfo.departmentId, Qt::UserRole);
personItem->setDepartmentInfo(departmentInfo);
ui->listView_department_person->setIndexWidget(departmentPersonModel->indexFromItem(item), personItem);
}
```
阅读全文