PDA

View Full Version : QAbstractTableModel subclass data() method implementation



kasper360
19th April 2011, 06:45
Hi,
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.


#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractTableModel>
#include <QSqlRecord>
#include <QList>


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

#endif


#include "mymodel.h"


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

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.

ChrisW67
19th April 2011, 06:55
Have you single-stepped through data() with your debugger? Have you checked that your rowCount() implementation is correct and not returning 0 when you are no expecting it?

kasper360
19th April 2011, 07:07
Thanks Chris for the quick reply. I haven't used the debugger on data() method yet, and I sure will do that. But about the rowCount() implementation, I'm sure that at some point it returns something other than 0 because I am adding stuff to my QMap in another part of my code. So recordMap->isEmpty() will be false and recordMap->value(row).size() will be returned.

kasper360
19th April 2011, 10:27
I used the debugger on data() method, but strangely, it's never even called. Is there a particular reason for this?