zoukankan      html  css  js  c++  java
  • QML中ListView的几种数据模型

    在QML中,经常会用到ListView控件,我们主要用到MVC模式,下面介绍几种常用数据模型,其中包括QML和C++模型

    ListModel:

    ListModel是一个简单的ListElement容器,每个容器都包含数据角色。其中内容可以动态定义,也可以在QML中显式定义。

         ListModel {
            id:m_model
              ListElement {
                  name: "Bill Smith"
                  number: "555 3264"
                  color1:"red"
              }
              ListElement {
                  name: "John Brown"
                  number: "555 8426"
                  color1:"green"
              }
              ListElement {
                  name: "Sam Wise"
                  number: "555 0473"
                   color1:"blue"
              }
          }
            ListView
        {
             100
            height: 100
            model:m_model
            delegate: Text{
            color: color1
            text:name+":"+number}
        }

    ObjectModel
    当ObjectModel被用于视图的时候,视图不再需要委托,因为对象模型已经包含了可视化的委托(项)

         ObjectModel {
            id: itemModel
            Rectangle { height: 20;  80;
            Text{
                color: "red"
               text:"Bill Smith"+":"+"555 3264"
            }
    
            }
            Rectangle { height: 20;  80;
                Text{
                     color: "green"
                   text: "John Brown"+":"+"555 8426"
                }
            }
            Rectangle { height: 20;  80;
                Text{
                    color: "blue"
                   text:"Sam Wise"+":"+"555 0473"
                }
            }
        }
        ListView{
         100
        height: 100
        model:itemModel
        }

    C++中的QStringList作为数据模型

     QStringList a;
        a<<"Bill Smith"<<"John Brown"<<"Sam Wise";            //QStringList model
    然后在注册为上下文属性
    QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("name1",QVariant::fromValue(a));
        ListView{
         100
        height: 100
        model:name1
        delegate: Text{
        text:modelData}
        }

    C++中QList

      dataobject.h头文件
    class DataObject : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
        Q_PROPERTY(QString number READ number WRITE setNumber NOTIFY numberChanged)
    public:
        DataObject(QObject *parent = nullptr);
        DataObject(const QString &name,const QString &color,const QString &number,QObject *parent=nullptr);
        QString name()const;
        void setName(const QString &name);
        QString color()const;
        void  setColor(const QString &color);
        QString number()const;
        void setNumber(const QString &number);
    
    signals:
        void nameChanged();
        void colorChanged();
        void numberChanged();
    private:
        QString m_name;
        QString m_color;
        QString m_number;
    };
    
    dataobject.cpp源文件
    #include "dataobject.h"
    
    DataObject::DataObject(QObject *parent) : QObject(parent)
    {
    
    }
    
    DataObject::DataObject(const QString &name, const QString &color, const QString &number, QObject *parent)
        :QObject(parent),m_name(name),m_color(color),m_number(number)
    {
    
    }
    
    QString DataObject::name() const
    {
        return m_name;
    }
    
    void DataObject::setName(const QString &name)
    {
        if(name!=m_name)
            m_name=name;
        emit nameChanged();
    }
    
    QString DataObject::color() const
    {
        return m_color;
    }
    
    void DataObject::setColor(const QString &color)
    {
        if(color!=m_color)
            m_color=color;
        emit colorChanged();
    }
    
    QString DataObject::number() const
    {
        return m_number;
    }
    
    void DataObject::setNumber(const QString &number)
    {
        if(number!=m_number)
            m_number=number;
        emit numberChanged();
    }

    在main.cpp中注册类:

     QList<QObject*>dataList;    //QObject model    
        dataList << new DataObject("Bill Smith","red","555 3264")<<new DataObject("John Brown","green","555 8426")
               <<new DataObject("Sam Wise","blue","555 0473");
        engine.rootContext()->setContextProperty("myObjectModel",QVariant::fromValue(dataList));

    在QML进行调用

        ListView{
         100
        height: 100
        model:myObjectModel
        delegate: Text{
        color: model.modelData.color
        text:name+":"+number}
        }

    C++中继承于QAbstractListModel作为数据模型

    自定义类AbstractListModel

    abstractlistmodel.h

    class  AbstractList   //抽象数据列表类
    {
       public:
        AbstractList(const QString &name,const QString &color,const QString &number);
        QString name() const;
        QString color() const;
        QString number() const;
    private:
        QString m_name;
        QString m_color;
        QString m_number;
    
    };
    class AbstractListModel : public  QAbstractListModel
    {
        Q_OBJECT
    public:
        enum AbstractListRoles{
             NameRole=Qt::UserRole+1,
            ColorRole,
            NumberRole
        };                               //定义抽象类成员角色
        AbstractListModel(QObject *parent=nullptr);
        void addList(const AbstractList &list);
        int rowCount(const QModelIndex &parent=QModelIndex()) const;  //返回给定父项行数
        QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;//返回索引所在项给定角色的存储数据
    protected:
        QHash<int,QByteArray> roleNames() const;  //返回模型的角色名
    private:
        QList<AbstractList> m_abstractList;     //抽象列表类容器
    };

    abstractlistmodel.cpp

    AbstractListModel::AbstractListModel(QObject *parent)
        :QAbstractListModel(parent)
    {
    
    }
    
    void AbstractListModel::addList(const AbstractList &list)
    {
        beginInsertRows(QModelIndex(),rowCount(),rowCount());
        m_abstractList.append(list);
    //    m_abstractList<<list;
        endInsertRows();
    }
    
    int AbstractListModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return m_abstractList.count();
    }
    
    QVariant AbstractListModel::data(const QModelIndex &index, int role) const
    {
      if(index.row()<0||index.row()>=m_abstractList.count())
          return QVariant();
      const AbstractList &abstractList=m_abstractList[index.row()];
      if(role==AbstractListRoles::NameRole)
          return abstractList.name();
      else if(role==AbstractListRoles::ColorRole)
          return abstractList.color();
      else if(role==AbstractListRoles::NumberRole)
              return abstractList.number();
      return QVariant();
    }
    
    QHash<int, QByteArray> AbstractListModel::roleNames() const
    {
        QHash<int,QByteArray> roles;
        //use operator[]
    //    roles[AbstractListRoles::NameRole]="name";            //定义对应角色值
    //    roles[AbstractListRoles::ColorRole]="color1";
    //    roles[AbstractListRoles::NumberRole]="number";
        //use insert
        roles.insert(AbstractListRoles::NameRole,"name");
        roles.insert(AbstractListRoles::ColorRole,"color1");
        roles.insert(AbstractListRoles::NumberRole,"number");
        return roles;
    }
    
    AbstractList::AbstractList(const QString &name, const QString &color, const QString &number)
        :m_name(name),m_color(color),m_number(number)
    {
    
    }
    
    QString AbstractList::name() const
    {
        return m_name;
    }
    
    QString AbstractList::color() const
    {
        return m_color;
    }
    
    QString AbstractList::number() const
    {
        return m_number;
    }

    main.cpp

    AbstractListModel listmodel;
        listmodel.addList(AbstractList("Bill Smith","red","555 3264"));
        listmodel.addList(AbstractList("John Brown","green","555 8426"));
        listmodel.addList(AbstractList("Sam Wise","blue","555 0473"));
        QQmlApplicationEngine engine;
       engine.rootContext()->setContextProperty("myModel",&listmodel);

    qml中调用:

     ListView{
             100
            height: 100
            model:myModel
            delegate: Text{
            color: color1
            text:name+":"+number}
        }

    以上5中方法中前两种主要是qml中的数据模型,后三种主要是C++作为数据模型

    转载请标明出处:https://blog.csdn.net/qq_35173114/article/details/80873842
    源码:https://download.csdn.net/download/qq_35173114/10511808

  • 相关阅读:
    #sort 快速排序 20. 9.14
    #Trie Trie树调试模板 20.09.21
    #operator ——“Kruskal算法求最小生成树 中的 operator” ~20.8.17
    #STL #List 容器函数以及一些内置函数的用法
    刷题周记(三)——#最小生成树:Kruskal#二分图:染色法、匈牙利算法#拓扑#DFS:排列数字、n-皇后#BFS:走迷宫、八格码#List容器
    #周测 9 —— 高低位交换、Subsequences Summing to Sevens S、积木大赛、跳石头
    刷题周记(二)——KMP,Trie,最大异或对,(并查集)合并集合、连通块中点的数量、食物链,堆排序,单多源最短路、Dijkstra、bellman-ford、SPFA、Floyd、(堆优化)Prim
    4.SQL(数据库变更)
    3.SQL(查询)
    2.Oracle基本使用
  • 原文地址:https://www.cnblogs.com/wxmwanggood/p/11038042.html
Copyright © 2011-2022 走看看