QML学习笔记4(qml ListView QAbstractListModel)

1,创建对象实体类
 

//person.h
#ifndef PERSON_H
#define PERSON_H

#include 

class Person : public QObject
{
    Q_OBJECT
public:
    Person(QString name,int age, QObject *parent = nullptr);
    Person(QObject *parent = nullptr);
    QString getName() const;
    void setName(const QString &value);

    int getAge() const;
    void setAge(int value);

    int getID() const;
    void setID(int value);

private:
    QString Name;
    int Age;
    int ID;
};

#endif // PERSON_H
//person.cpp
#include "person.h"

Person::Person(QString name,int age,QObject *parent) : QObject(parent)
{
    Name=name;
    Age=age;
}

Person::Person(QObject *parent): QObject(parent)
{

}

QString Person::getName() const
{
    return Name;
}

void Person::setName(const QString &value)
{
    Name = value;
}

int Person::getAge() const
{
    return Age;
}

void Person::setAge(int value)
{
    Age = value;
}

int Person::getID() const
{
    return ID;
}

void Person::setID(int value)
{
    ID = value;
}

 2,创建基于QAbstractListModel的数据列表
 

//personlistviewmodel.h
#ifndef PERSONLISTVIEWMODEL_H
#define PERSONLISTVIEWMODEL_H

#include 
#include 
#include 
#include "person.h"
#include "sqlitehelper.h"
#include "sqlite3.h"

class PersonListViewModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit PersonListViewModel(SQLiteHelper *sqlhelper,QObject *parent = nullptr);
    ~PersonListViewModel();
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const ;
    virtual QHash roleNames() const ;
    virtual int rowCount(const QModelIndex &parent = QModelIndex())  const ;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;


    int InsertPerson(Person *person);
    void deletePerson(int id);
    Q_INVOKABLE void updataPerson(int id, QString name, int age);
    Person* SelectPerson(int id);
    QList getAllPerson();
    QList personList;

private:
    enum DataRoles{
        NameRole,
        AgeRole,
        IDRole
    };

    SQLiteHelper *sqlHelper;
public slots:
    void  addItem(QString name,int age);
    void  deleteItem(int id);
};

#endif // PERSONLISTVIEWMODEL_H
//personlistviewmodel.cpp
#include "personlistviewmodel.h"

PersonListViewModel::PersonListViewModel(SQLiteHelper *sqlhelper, QObject *parent)
{
    Q_UNUSED(parent);
    sqlHelper=sqlhelper;

    //创建数据库
    char *errmsg;      //出错信息
    const char *createPerson="create table IF NOT EXISTS Person("
                             "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                             "Name varchar(30) NOT NULL, "
                             "Age int NOT NULL"
                             ");";
    int res = sqlite3_exec(sqlHelper->sqldb, createPerson, NULL, NULL, &errmsg);
    if(res!=SQLITE_OK)
    {
        qDebug()<<"创建Person表失败:"<sqldb)<sqldb)<getID();
    case  DataRoles::AgeRole : return personList[index.row()]->getAge();
    case  DataRoles::NameRole : return personList[index.row()]->getName();
    default : break ;
    }
    return QVariant();
}

QHash PersonListViewModel::roleNames() const
{
    QHash  temp ;
    temp.insert(DataRoles :: IDRole,"id");
    temp.insert(DataRoles :: AgeRole,"age");
    temp.insert(DataRoles :: NameRole,"name");
    return temp ;
}

int PersonListViewModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return personList.length();
}

bool PersonListViewModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    int row=index.row();
    int col=index.column();
    auto p=index.parent();
    auto v=value.isValid();
   if (!hasIndex(row,col, p) || !v)
        return false;

    Person *person = personList[index.row()];
    if (role == DataRoles::NameRole)
    {
        person->setName(value.toString());
    }
    else if (role == DataRoles::AgeRole)
    {
        person->setAge(value.toString().toInt());
    }
    else
    {
        return false;
    }

    emit dataChanged(index, index, { role } );

    return true ;
}

int PersonListViewModel::InsertPerson(Person *person)
{
    char *errmsg;
    QString insertPerson=QString("INSERT INTO Person(NAME,AGE) VALUES('%1', %2)").arg(person->getName()).arg(person->getAge());
    int res=sqlite3_exec(sqlHelper->sqldb, insertPerson.toUtf8(), NULL, NULL, &errmsg);
    if(res!=SQLITE_OK)
    {
        qDebug()<sqldb)<sqldb)<sqldb, selectPerson.toUtf8(), &pResult, &nrow, &ncolumn, &zErrMsg);
        if(result==SQLITE_OK)
        {
            return  sqlHelper->getIntValue(pResult,ncolumn,0,0);
        }
        else
        {
            return  -1;
        }
    }
}

void PersonListViewModel::deletePerson(int id)
{
    char *errmsg;
    QString insertPerson=QString("DELETE FROM Person WHERE ID=%1").arg(id);
    int res=sqlite3_exec(sqlHelper->sqldb, insertPerson.toUtf8(), NULL, NULL, &errmsg);
    if(res!=SQLITE_OK)
    {
        qDebug()<sqldb)<sqldb)<sqldb, insertPerson.toUtf8(), NULL, NULL, &errmsg);
    if(res!=SQLITE_OK)
    {
        qDebug()<sqldb)<sqldb)<0)
        {
            qDebug()<<"ID"<getID()==id)
                {
                    if(personList[i]->getAge()!=age)
                    {
                        setData(this->index(i) ,age,DataRoles::AgeRole);
                    }
                    if(personList[i]->getName()!=name)
                    {
                        setData(this->index(i),name,DataRoles::NameRole);
                    }

                    //personList.replace(i,personList[i]); //beginRemoveRows(QModelIndex(),i,i);


                    //endRemoveRows();
                    break;
                }
            }
        }
    }
}

Person *PersonListViewModel::SelectPerson(int id)
{
    char * zErrMsg = NULL;      //错误信息指针的地址
    char ** pResult = NULL;     //用来指向sql执行结果的指针
    int nrow = 0;               //满足条件的记录数目
    int ncolumn = 0;            //每条记录包含的字段数据
    QString selectPerson= QString("SELECT * FROM Person WHERE ID=%1").arg(id);
    int result = sqlite3_get_table(sqlHelper->sqldb, selectPerson.toUtf8(), &pResult, &nrow, &ncolumn, &zErrMsg);
    if(SQLITE_OK != result)
    {
        printf("执行sql语句失败\n");
        return nullptr;
    }
    else
    {
        Person *person=new Person;
        person->setID(sqlHelper->getIntValue(pResult,ncolumn,0,0));
        person->setName(sqlHelper->getQStringValue(pResult,ncolumn,0,1));
        person->setAge(sqlHelper->getIntValue(pResult,ncolumn,0,2));
        sqlite3_free_table(pResult);
        return  person;
    }
}

QList PersonListViewModel::getAllPerson()
{
    QList persons;
    char * zErrMsg = NULL;      //错误信息指针的地址
    char ** pResult = NULL;     //用来指向sql执行结果的指针
    int nrow = 0;               //满足条件的记录数目
    int ncolumn = 0;            //每条记录包含的字段数据
    QString selectPerson= QString("SELECT * FROM Person");
    int result = sqlite3_get_table(sqlHelper->sqldb, selectPerson.toUtf8(), &pResult, &nrow, &ncolumn, &zErrMsg);
    if(SQLITE_OK != result)
    {
        qDebug()<<"执行sql语句失败"<setID(sqlHelper->getIntValue(pResult,ncolumn,i,0));
            person->setName(sqlHelper->getQStringValue(pResult,ncolumn,i,1));
            person->setAge(sqlHelper->getIntValue(pResult,ncolumn,i,2));
            persons.append(person);
        }
        sqlite3_free_table(pResult);
    }
    return persons;
}

void PersonListViewModel::addItem(QString name, int age)
{
    Person *person=new Person(name,age);
    auto count=InsertPerson(person);
    qDebug()<<"count:"<setID(count);
    beginInsertRows(QModelIndex(),rowCount(),rowCount());
    personList.insert(personList.length(),person);
    endInsertRows();
}

void PersonListViewModel::deleteItem(int id)
{

    deletePerson(id);
    if(personList.count()>0)
    {
        qDebug()<<"ID"<getID()==id)
            {
                beginRemoveRows(QModelIndex(),i,i);
                personList.removeAt(i);
                endRemoveRows();
                break;
            }
        }
    }

}

需要注意的地方:

1, 需要实现data,roleNames,rowCount重写

roleNames方法中,id,age,name用来在qml中调用 
temp.insert(DataRoles :: IDRole,"id");
    temp.insert(DataRoles :: AgeRole,"age");
    temp.insert(DataRoles :: NameRole,"name");

2,要实现修改数据,需要重写方法setData方法

3,枚举类,用来关联到实体对象的信息,类似于表格的列
enum DataRoles{
        NameRole,
        AgeRole,
        IDRole
    };

3,注册类并使用

QML学习笔记3(qml注册有参单例)https://blog.csdn.net/dwm88888888/article/details/131327605

你可能感兴趣的:(QT,学习,笔记,算法)