其他分享
首页 > 其他分享> > Fetch More Example

Fetch More Example

作者:互联网

 

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