QAbstractTableModel - how to retrieve internal model item



  • Hi guys,

    I'm using a custom implementation of "QAbstractTableModel" to represent a list of <Workout> object
    See picture below :
    https://www.dropbox.com/s/o5aio3gqj745xat/workoutListe.png

    When I double click an item, I retrieve the current object with :
    @std::shared_ptr<Workout> getWorkoutAtRow(const QModelIndex &index);@
    That return me one of the <Workout> in the QList of the model

    This system works fine, until I sort or filter the view, the QModelIndex.row() is now different from my model data (QList), so I return the wrong object:
    Example of this happening :
    https://www.dropbox.com/s/u0ihr7li4f74bif/reorderList.png
    When I open this element, it will open the QList<Workout>.at(0), but this is not the good object!
    @// ------------------------------------------------------------------------------------------
    std::shared_ptr<Workout> WorkoutTableModel::getWorkoutAtRow(const QModelIndex &index) {

    std::shared_ptr<Workout> work( lstWorkout.at(index.row()) );
    return work;
    

    }@

    I heard you can use QPersistentModelIndex to fix this issue, but I haven't seen any code or example using it. Being new to QT and C++ (I come from Java), i'm kind of stuck on this problem..
    If anyone ever experienced this and could give me an hand, would be really appreciated!

    Here is the complete code :

    WorkoutTableModel.H
    @#ifndef MYTABLEMODEL_H
    #define MYTABLEMODEL_H

    #include <QAbstractTableModel>
    #include <QPersistentModelIndex>
    #include "workout.h"

    class WorkoutTableModel : public QAbstractTableModel {
    Q_OBJECT

    public:
    WorkoutTableModel(QObject *parent = 0);
    WorkoutTableModel(QList<std::shared_ptr<Workout>>, QObject *parent = 0);

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    
    
    std::shared_ptr<Workout> getWorkoutAtRow(const QModelIndex &index);
    

    private:
    QList<std::shared_ptr<Workout>> lstWorkout;
    // QList<QPersistentModelIndex> lstPersistentModelIndex;

    };

    #endif // MYTABLEMODEL_H@

    WorkoutTableModel.cpp
    @#include "workouttablemodel.h"
    #include "workout.h"
    #include <QDebug>

    WorkoutTableModel::WorkoutTableModel(QObject *parent) : QAbstractTableModel(parent) {
    }

    // ------------------------------------------------------------------------------------------
    WorkoutTableModel::WorkoutTableModel(QList<std::shared_ptr<Workout>> lstWorkout, QObject *parent) : QAbstractTableModel(parent) {
    // beginResetModel();
    this->lstWorkout = lstWorkout;
    // endResetModel();
    }

    // ------------------------------------------------------------------------------------------
    std::shared_ptr<Workout> WorkoutTableModel::getWorkoutAtRow(const QModelIndex &index) {

    std::shared_ptr<Workout> work( lstWorkout.at(index.row()) );
    return work;
    

    }

    // ------------------------------------------------------------------------------------------
    int WorkoutTableModel::rowCount(const QModelIndex &parent) const {

    Q_UNUSED(parent);
    return lstWorkout.size();
    

    }

    // ------------------------------------------------------------------------------------------
    int WorkoutTableModel::columnCount(const QModelIndex &parent) const {

    Q_UNUSED(parent);
    return 5;
    

    }

    // ------------------------------------------------------------------------------------------
    QVariant WorkoutTableModel::data(const QModelIndex &index, int role) const {

    qDebug() << "method DATA called! -----" << index.row() << " role -- " << role;
    
    
    if (!index.isValid())
        return QVariant();
    
    if (index.row() >= lstWorkout.size() || index.row() < 0)
        return QVariant();
    
    if (role == Qt::DisplayRole) {
        std::shared_ptr<Workout> work = lstWorkout.at(index.row());
    
        if (index.column() == 0)  {
            return work->getName();
        }
        else if(index.column() == 1) {
            return work->getCreatedBy();
        }
        else if(index.column() == 2) {
            return (Workout::getTypeToString(work->getType()) );
        }
        else if(index.column() == 3) {
            return work->getTotalLength();
        }
        else if(index.column() == 4) {
            return work->getMaxPower();
        }
        else {
            return "column not defined";
        }
    }
    return QVariant();
    

    }

    // --------------------------------------------------------------------------------------------
    QVariant WorkoutTableModel::headerData(int section, Qt::Orientation orientation, int role) const {

    if (role != Qt::DisplayRole)
        return QVariant();
    
    if (orientation == Qt::Vertical) {
        return QString::number(section);
    }
    else {
        switch (section) {
        case 0 :
            return "Name";
            break;
        case 1:
            return "Created by";
            break;
        case 2:
            return "Type";
            break;
        case 3:
            return "Length (minutes)";
            break;
        case 4:
            return "Max power";
            break;
        default :
            return "horizontal";
    
        }
    }
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.