PDA

View Full Version : QTableView Model Help



tntcoda
22nd June 2008, 15:14
Hi,

I'm having some problems working out how to implement the data() function from QAbstractTableModel for use in a QTableView model.

My data is stored in a large vector, that the model (which is read only) has a pointer to, I set up the headerData() function, so all the column names are correct and that works fine. rowCount and ColumnCount are also in my class, correct i think.

I have 4 columns, and x number or rows, so i just need some help with returning the correct data from the data() function.

Here is my code, im basically not sure how i use the index and role values in order to know a) which column the data is from, and b) the row number. I need these 2 values in order to know which data to get from the data vector. For example if its from column 2 and row 5, i know i need to return the subject from element 5 in the vector. Is it as simple as role is the column and index is the row? :confused:



QVariant QueueModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();

//return ....
}


Also, do i have to do anything special to keep the view updating? i.e if i change a value in the data vector and another point in my program (outside of the model), will the view just update automatically?

Thanks alot for any tips,

Jack

janus
22nd June 2008, 15:33
Hi,

you can use index.row() and index.column().

If you want to return "default" values you return
return QSqlQueryModel(or the base class of your model)::data(index, role)

tntcoda
22nd June 2008, 15:41
Thanks alot, that makes sense so i can just return a QString from data() depending on the row/col which is great.

It seems though that the data() function isnt being called at all? The model has 0 rows to start with, just columns so i suppose there is no need to get any data.

However when i add data to the vector from another class, the model/view needs to update and request data, any pointers on how i can do this?

This is my model class:


class QueueModel : public QAbstractTableModel
{
Q_OBJECT

public:
QueueModel(QWidget * parent, queue *q);

int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;

// Returns data for specified index
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

// For putting labels on header columns
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

private:
queue *que; // rows = que->GetCount()
int colCount; // Number of columns
};


Problem is data is never called :(

Arghargh
23rd June 2008, 06:57
Thanks alot, that makes sense so i can just return a QString from data() depending on the row/col which is great.

It seems though that the data() function isnt being called at all? The model has 0 rows to start with, just columns so i suppose there is no need to get any data.

However when i add data to the vector from another class, the model/view needs to update and request data, any pointers on how i can do this?

Problem is data is never called :(

Hello, you could try to add the data by using the setData() method of your model (you would have to reimplement it). It that method, you then emit the dataChanged() signal to update your view.

Arghargh