我已经实现了QIdentityProxyModel这样的:

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;
};

当我在sourceModel()中插入索引比entriesPerPage多的行时, View 显示空行,因此行号比entriesPerPage多,尽管rowCount()返回的数字等于entriesPerPage

c&#43;&#43; - QIdentityProxyModel显示空行-LMLPHP

我如何摆脱空行?

最佳答案

第一。重写rowCount / mapFromSource是不良做法。我建议您使用QIdentityProxyModel以获得更清晰的代码。

主要。您的问题出在QAbstractProxyModel / getEntriesPerPage方法中。更新此类数据后,您需要调用setPage / beginResetModel

inline void setPage(const qint64 &value)
{
    beginResetModel();
    page = value;
    endResetModel();
}

题外话:这很酷,您必须在BSUIR中使用Qt进行编码。你的老师是谁?

09-28 08:45