首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QIdentityProxyModel显示空行

QIdentityProxyModel显示空行
EN

Stack Overflow用户
提问于 2017-06-12 16:56:29
回答 0查看 511关注 0票数 1

我已经像这样实现了QIdentityProxyModel

代码语言:javascript
复制
class ProxyModel : public QIdentityProxyModel
{
    Q_OBJECT
public:
    ProxyModel(QObject *parent)
    {
        entriesPerPage = 3;
        page = 1;
    }

    inline int getEntriesPerPage() const
    { return entriesPerPage; }
    inline void setEntriesPerPage(int value)
    { entriesPerPage = value; }

    inline qint64 getPage() const
    { return page; }
    inline void setPage(const qint64 &value)
    { page = value; }

    inline int rowCount(const QModelIndex &parent = QModelIndex()) const override{
        Q_UNUSED(parent)
        if(!sourceModel())
            return 0;

        return entriesPerPage * page <= sourceModel()->rowCount()
                ? entriesPerPage
                : sourceModel()->rowCount() - entriesPerPage * (page - 1);
    }

    inline int columnCount(const QModelIndex &parent = QModelIndex()) const override{
        Q_UNUSED(parent);
            return 6;
    }

    QModelIndex ProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
    {
        if(!sourceModel() && !proxyIndex.isValid())
            return QModelIndex();

        return sourceIndex.isValid()
                ? createIndex(sourceIndex.row() % entriesPerPage, sourceIndex.column(), sourceIndex.internalPointer())
                : QModelIndex();
    }

    QModelIndex ProxyModel::mapToSource(const QModelIndex &proxyIndex) const
    {
        if(!sourceModel() && !proxyIndex.isValid())
            return QModelIndex();

        QModelIndex remapped = createIndex(proxyIndex.row() ,
                                           proxyIndex.column(),
                                           proxyIndex.internalPointer());
        return QIdentityProxyModel::mapToSource(remapped);
    }

private:
    int entriesPerPage;
    qint64 page;
};

当我在索引大于entriesPerPage的情况下在sourceModel()中插入一行时,视图显示空行,因此行数大于entriesPerPage,尽管rowCount()返回的number等于entriesPerPage

怎样才能去掉空行?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44495253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档