Qt中Model与List的使用

class PlaylistModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    enum Column
    {
        Title = 0,
        ColumnCount
    };

    PlaylistModel(QObject *parent = 0);

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &child) const;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    QMediaPlaylist *playlist() const;
    void setPlaylist(QMediaPlaylist *playlist);

    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);

private slots:
    void beginInsertItems(int start, int end);
    void endInsertItems();
    void beginRemoveItems(int start, int end);
    void endRemoveItems();
    void changeItems(int start, int end);

private:
    QMediaPlaylist *m_playlist;
    QMap<QModelIndex, QVariant> m_data;
};

代码来自QT示例程序Media Player Example

Model是QAbstractItemModel的继承类,List是QMediaPlaylist。
Model本身不存储数据,但有一个List*成员,List内存储数据,每个List条目对应一个ModelIndex,可用index获取。

Model为List提供了访问、修改等接口,并对每个条目增设了一些额外信息。

Model内有一个值对表QMap,为每个数据条目提供额外的信息,可用data获取,用setData添加。

使用Model时先setPlaylist:

void PlaylistModel::setPlaylist(QMediaPlaylist *playlist)
{
    if (m_playlist) {
        disconnect(m_playlist, SIGNAL(mediaAboutToBeInserted(int,int)), this, SLOT(beginInsertItems(int,int)));
        disconnect(m_playlist, SIGNAL(mediaInserted(int,int)), this, SLOT(endInsertItems()));
        disconnect(m_playlist, SIGNAL(mediaAboutToBeRemoved(int,int)), this, SLOT(beginRemoveItems(int,int)));
        disconnect(m_playlist, SIGNAL(mediaRemoved(int,int)), this, SLOT(endRemoveItems()));
        disconnect(m_playlist, SIGNAL(mediaChanged(int,int)), this, SLOT(changeItems(int,int)));
    }

    beginResetModel();    //inherited:对underlying data大批量修改时先调用此函数
    m_playlist = playlist;

    if (m_playlist) {
        connect(m_playlist, SIGNAL(mediaAboutToBeInserted(int,int)), this, SLOT(beginInsertItems(int,int)));
        connect(m_playlist, SIGNAL(mediaInserted(int,int)), this, SLOT(endInsertItems()));
        connect(m_playlist, SIGNAL(mediaAboutToBeRemoved(int,int)), this, SLOT(beginRemoveItems(int,int)));
        connect(m_playlist, SIGNAL(mediaRemoved(int,int)), this, SLOT(endRemoveItems()));
        connect(m_playlist, SIGNAL(mediaChanged(int,int)), this, SLOT(changeItems(int,int)));
    }

    endResetModel();    /inherited:修改结束再次调用

上述5个槽函数将list的变化传递给model,如:


void PlaylistModel::beginInsertItems(int start, int end)
{
    m_data.clear();    //每当list条目增加、删减、变化时将Model内条目的值对表清空,并将这些变化传递给model
    beginInsertRows(QModelIndex(), start, end);
}

其他几个槽函数都类似

下面对几个接口进行简述

int PlaylistModel::rowCount(const QModelIndex &parent) const
{    //返回条目数,m_playlist在构造函数中赋值0,在列表中parent为root元素所以为空
    return m_playlist && !parent.isValid() ? m_playlist->mediaCount() : 0;
}
QModelIndex PlaylistModel::index(int row, int column, const QModelIndex &parent) const
{    //根据list中的行与列返回model中对应的modelindex
     //若给出的行与列有效则返回相应modelindex,否则返回空元素
    return m_playlist && !parent.isValid()
            && row >= 0 && row < m_playlist->mediaCount()
            && column >= 0 && column < ColumnCount
        ? createIndex(row, column)
        : QModelIndex();
}
QModelIndex PlaylistModel::parent(const QModelIndex &child) const
{    //在list中parent必为空
    Q_UNUSED(child);    //宏告诉编译器child参数用不到,避免警告

    return QModelIndex();
}

bool PlaylistModel::setData(const QModelIndex &index, const QVariant &value, int role)
{    //在值对表中为条目添加额外信息
    Q_UNUSED(role);
    m_data[index] = value;
    emit dataChanged(index, index);
    return true;
}
QVariant PlaylistModel::data(const QModelIndex &index, int role) const
{    //当role正确时,从值对表中获取条目的额外信息;当额外信息不存在时,返回条目对应文件信息作为额外信息
    if (index.isValid() && role == Qt::DisplayRole) {
        QVariant value = m_data[index];
        if (!value.isValid() && index.column() == Title) {
            QUrl location = m_playlist->media(index.row()).canonicalUrl();
            return QFileInfo(location.path()).fileName();
        }

        return value;
    }
    return QVariant();
}

model存储数据结构如下,list类型的model可视为treeModel的特殊情况,所以上述parent参数为0(root item)

QModelIndex indexA= model->index(0,0,QModelIndex());

QModelIndex indexC = model->index(2,1,QModelIndex());

QModelIndex indexB= model->index(1,0, indexA);

Qt中Model与List的使用

//程序中QAbstractItemModel相关接口:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
//获取子项目的额外信息
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
//设置子项目的额外信息
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
//对底层数据进行reset前使用
[protected] void QAbstractItemModel::beginResetModel()
//底层数据reset完毕后使用
[protected] void QAbstractItemModel::endResetModel()
//对底层数据增加条目前后,删减条目前后使用
[protected] void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
[protected] void QAbstractItemModel::endInsertRows()
[protected] void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)
[protected] void QAbstractItemModel::endRemoveRows()