1

使用 qqmllistproperty 通过 listview 显示数据库中的记录

我正在尝试通过 qqmllistproperty 显示数据库中的记录,但它只显示最后的记录

DataManager::DataManager(QObject *parent) : QObject(parent)
{

}
QQmlListProperty<EmployeeDetails> DataManager::employeedetailslist()
{
    return QQmlListProperty<EmployeeDetails>(this, m_employeeDetailsList);
}

void DataManager::setEmployeeDetailsList(const QList<EmployeeDetails *> &employeeDetailsList)
{
    m_employeeDetailsList = employeeDetailsList;
}

void DataManager::printAllPersons(){
    m_employeeDetailsList.clear();
    EmployeeDetails *em = new EmployeeDetails();
    QSqlQuery query("SELECT * FROM employee");
    int idName = query.record().indexOf("name");
    while (query.next())
    {
       em->setName( query.value(idName).toString());
       m_employeeDetailsList.append(em);

    }
    int count  = m_employeeDetailsList.count();
    for (int i =0;i<count;i++) {
        qDebug()<<"name"<<m_employeeDetailsList[i];

    }
    qDebug() <<"count"<<count;
//    qDebug() <<"name"<<m_employeeDetailsList.;
    emit listchanged();


}

有 9 条记录,我希望所有 9 条记录都应该出现在列表视图中,但它在列表视图中显示最后一条记录 9 次

4

0 回答 0