[Qt-interest] QTreeView not calling canFetchMore()

Christiaan Dekter cdekter at gmail.com
Tue May 11 08:26:24 CEST 2010


I am implementing a flat list view using a QTreeView. The view will
potentially contain over a million records so I am trying to implement lazy
loading. What I'm trying to achieve it to load say 50k items at a time as
required. I have followed the fetchmore example in the QT docs but my
QTreeView is simply not calling canFetchMore() or fetchMore().

Here is my model class. You can see it contains some optimisations that are
possible because it only has one level of items.

LogFileModel::LogFileModel(LogFile* logFile, QObject* parent):
QAbstractItemModel(parent) {
    m_logFile = logFile;
    m_rootItem = new LogEntry();
    setupModelData();
}

LogFileModel::~LogFileModel() {
    delete m_rootItem;
}

void LogFileModel::setupModelData() {
    QStringList lines = m_logFile->readBlock();
    LogEntry* entry;
    int offset = m_logFile->line() - lines.count() + 1;

    for (int i=0; i < lines.length(); i++) {
        entry = m_logFile->parser()->parseLine(i + offset, lines[i],
m_rootItem);
        m_rootItem->appendChild(entry);
    }

}

QModelIndex LogFileModel::index(int row, int column, const QModelIndex&
parent) const {
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    if (!parent.isValid())
        return createIndex(row, column);

    return QModelIndex();
}

QModelIndex LogFileModel::parent(const QModelIndex& index) const {
    return QModelIndex();
}

int LogFileModel::rowCount(const QModelIndex& parent) const {
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid())
        return m_rootItem->childCount();
    else
        return 0;
}

int LogFileModel::columnCount(const QModelIndex& /* parent */) const {
    return m_logFile->parser()->columnCount() + 1; // extra column for line
num
}

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

    if (role != Qt::DisplayRole)
        return QVariant();

    LogEntry* item = m_rootItem->child(index.row());
    return item->data(index.column());
}

Qt::ItemFlags LogFileModel::flags(const QModelIndex& index) const {
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled|Qt::ItemIsSelectable;
}

QVariant LogFileModel::headerData(int section, Qt::Orientation orientation,
int role) const {
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if (section == 0)
            return QVariant(i18n("Line"));
        else
            return m_logFile->parser()->headerData(section - 1);
    }

    return QVariant();
}

void LogFileModel::fetchMore(const QModelIndex& /* parent */) {
    QStringList lines = m_logFile->readBlock();
    LogEntry* entry;
    int offset = m_logFile->line() - lines.count() + 1;
    int linesRead = lines.count();

    beginInsertRows(QModelIndex(), m_rootItem->childCount(),
m_rootItem->childCount() + linesRead);

    for (int i = 0; i < lines.length(); i++) {
        entry = m_logFile->parser()->parseLine(i + offset, lines[i],
m_rootItem);
        m_rootItem->appendChild(entry);
    }

    endInsertRows();
}

bool LogFileModel::canFetchmore(const QModelIndex& parent) const {
    QLOG_DEBUG() << "canFetchmore() called";
    if (!parent.isValid())
        return !m_logFile->atEnd();

    return false;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100511/a06db99d/attachment.html 


More information about the Qt-interest-old mailing list