ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Fetch More Example

2021-04-08 12:01:11  阅读:186  来源: 互联网

标签:infosPtr const QModelIndex Example TestModel return row Fetch More


 

TestModel.h
class TestModel : public QAbstractItemModel
{
    Q_OBJECT

public:

    enum Columns {Number, Group, Sn, DeviceSN, AuthObject, AuthProduct, RecoveryTime, ColumnSize};

    TestModel(QObject *parent = nullptr)
    {
        m_headers << tr("No.") << tr("Group") << tr("Serial Number") << tr("Device SN")
                  << tr("Authorized User") << tr("Product") << tr("Time Restored");
    }
    virtual QModelIndex index(int row, int column, const QModelIndex &parent) const Q_DECL_OVERRIDE;
    virtual QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    virtual int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;

signals:
    void numberPopulated(int number);

public slots:
    void setData(const QList<Data::ActiveInfoPtr> &infosPtr);

protected:
    bool canFetchMore(const QModelIndex &parent) const override;
    void fetchMore(const QModelIndex &parent) override;

private:
    QList<Data::ActiveInfoPtr> m_infosPtr;
    int fileCount;
    QStringList m_headers;
};

 

 

TestModel.cpp
void TestModel::setData(const QList<Data::ActiveInfoPtr> &infosPtr)
{
    beginResetModel();
    m_infosPtr = infosPtr;
    fileCount = 0;
    endResetModel();
}

bool TestModel::canFetchMore(const QModelIndex &parent) const
{
    if (parent.isValid())
        return false;
    return (fileCount < m_infosPtr.size());
}

void TestModel::fetchMore(const QModelIndex &parent)
{
    if (parent.isValid())
        return;
    int remainder = m_infosPtr.size() - fileCount;
    int itemsToFetch = qMin(100, remainder);

    if (itemsToFetch <= 0)
        return;

    beginInsertRows(QModelIndex(), fileCount, fileCount + itemsToFetch - 1);

    fileCount += itemsToFetch;

    endInsertRows();

    emit numberPopulated(itemsToFetch);
}

QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    if (m_infosPtr.empty())
        return QModelIndex();

    if (!m_infosPtr.empty()) {
        ActiveInfoPtr info = m_infosPtr.at(static_cast<size_t>(row));
        if (info.isNull())
            return QModelIndex();

        return createIndex(row, column, info.data());
    }

    return QModelIndex();
}

QModelIndex TestModel::parent(const QModelIndex &child) const
{
    return QModelIndex();
}

int TestModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : fileCount;
}

int TestModel::columnCount(const QModelIndex &) const
{
    return Columns::ColumnSize;
}

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

    int row = index.row();
    int column = index.column();

    if (row >= m_infosPtr.size())
        return QVariant();

    switch (role) {
    case Qt::DisplayRole:
        if (column == Columns::Number)
            return m_infosPtr.at(row)->number();

        if (column == Columns::Group)
            return m_infosPtr.at(row)->group();

        if (column == Columns::Sn)
            return Utils::generateXXX(m_infosPtr.at(row)->sn());

        if (column == Columns::DeviceSN)
            return m_infosPtr.at(row)->deviceSn();

        if (column == Columns::AuthProduct)
            return Utils::authObject(m_infosPtr.at(row)->authProduct());

        if (column == Columns::AuthObject)
            return m_infosPtr.at(row)->authObject();

        if (column == Columns::RecoveryTime)
            return Utils::timeStampFormat(m_infosPtr.at(row)->recoveryTime());

        return QVariant();

    default:
        return QVariant();
    }
}

QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const
{

    if (Qt::Vertical == orientation)
        return QVariant();

    if (role == Qt::DisplayRole) {
        if (section >= m_headers.size())
            return QVariant();

        return m_headers.at(section);
    }
    return QVariant();

}

 

 

widget 中使用

m_listModel = new TestModel(this);
m_view->setModel(m_listModel);
m_listModel->setData(infosPtr);

 

 

 

-----

setData

标签:infosPtr,const,QModelIndex,Example,TestModel,return,row,Fetch,More
来源: https://www.cnblogs.com/qcwh736/p/14610178.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有