2
votes

I have subclassed a QAbstractTableModel to represent data from a QMap. This QMap has QLists of QSqlRecords and this map is modified by some other part of my code. I want to use this model with a QTableView to display the sql records in this map for each key. Here is my code.

//mymodel.h

class MyModel : public QAbstractTableModel
{
Q_OBJECT

public:
    MyModel(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;
    void setRecordMap(QMap<int, QList<QSqlRecord>> *map);
    void setSelectedSerMsgIndex(QModelIndex *index);

private:
    QMap<int, QList<QSqlRecord>> *recordMap;
    QModelIndex *selectedSerendibMsgIndex;
};

//mymodel.cpp

MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
{

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).size();
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).at(0).count();
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if(recordMap->isEmpty())
        return QVariant();

    if (!index.isValid())
        return QVariant();

    int row = selectedSerendibMsgIndex->row();
    if (index.row() >= recordMap->value(row).size())
        return QVariant();

    if (role == Qt::DisplayRole) 
    {
        return  recordMap->value(row).value(index.row()).value(index.column()); /* QVariant("hello");*/
    } 
    else 
    {
        return QVariant();
    }
}

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

void MyModel::setSelectedSerMsgIndex(QModelIndex *index)
{
    selectedSerendibMsgIndex = index;
}

Sorry for the huge post. But the problem is, I cannot see the data from the map. I am guessing it is because there's something wrong with my implementation of the data() method. But I can't figure out what it is. Please be kind enough to help me. Thank you.

1
Then I would check if rowCount and columnCount return the expected values (>0). Also call reset() after replacing the map in setRecordMap(). - Frank Osterfeld
As you pass the map by value, does the map change after you pass it to the model? - Frank Osterfeld
Another thing to check, just to be sure: have you set the model on the view? - Caleb Huitt - cjhuitt
No need to use pointers to QMap, either. Unfortunately, I don't think this is the issue. :( I'd suggest delegating to QSqlTableModel or QSqlQueryModel instead of mapping directly to QSqlRecord. Indexing, view refreshing and selection logic might break. - andref
kasper360: It won't work, as you can't observe a map for changes (there is no notification mechanism). And the model has to insert/remove rows and columns as data is inserted/removed into the map. You either need a private data structure that is controlled by the model (note that using maps there is rather inefficient - you want O(1) lookup), or a notification from the outside that the shared map has changed (and how). - Frank Osterfeld

1 Answers

1
votes

try changing this:

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

to this:

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    beginResetModel();
    recordMap = map;
    endResetModel();
}