找回密码
 立即注册
收起左侧

请教《快速入门》一书中16.2.2的问题

1
回复
6722
查看
[复制链接]
累计签到:13 天
连续签到:1 天
来源: 2017-2-9 14:46:20 显示全部楼层 |阅读模式
1Qter豆
在创建新模型中stringlistmodel.h、stringlistmodel.cpp文件中有以下问题不明白:程序中的role在哪赋值?



#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H

#include <QAbstractListModel>
#include <QStringList>

class StringListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    StringListModel(const QStringList &strings, QObject *parent = 0)
        : QAbstractListModel(parent), stringList(strings) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;// role是个输入的参数,这个参数在哪赋值?
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt:isplayRole) const;

private:
    QStringList stringList;
};

#endif // STRINGLISTMODEL_H



#include "stringlistmodel.h"

int StringListModel::rowCount(const QModelIndex &parent) const
{
    return stringList.count();
}

QVariant StringListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= stringList.size())
        return QVariant();

    if (role == Qt:isplayRole)
        return stringList.at(index.row());
    else
        return QVariant();
}

QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
                                     int role) const
{
    if (role != Qt:isplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return QString("Column %1").arg(section);
    else
        return QString("Row %1").arg(section);
}



回复

使用道具 举报

累计签到:13 天
连续签到:1 天
2017-2-16 10:37:33 显示全部楼层
已经解决了,各位!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了