Hello,
I have subclassed QSqlQueryModel but for the time my current model just creates a simple 3rowsx2columns table, as QStandardItemModel would do. Then I have also subclassed QTableView for 2 purposes:
- make items centered (I rewrite the viewOptions() method but nothing changed: the items are still not centered, as for the standard QTableView class)
- make the items editable: I use a so-called SpinBoxDelegate I found in the itemviews section of the examples, which works with a QStandardItemModel but not with my model, so I suppose that my model is not editable but how can I change it?
Thanks in advance for your help (code is below).


///////////////////////////////////////////////////////////////////////////////
// File : vadTableModel.h //
///////////////////////////////////////////////////////////////////////////////


#ifndef _VAD_TABLE_MODEL_H
#define _VAD_TABLE_MODEL_H

#include <qsqlquerymodel.h>
#include <qvector.h>
#include <visCore.h>

#define R 3
#define C 2

/** vadTableModel est la classe definissant le modele de donnees de type
table utilise dans le cadre de l'applciation de visualisation
d'informations dans le projet de VAD.
*/
class vadTableModel : public QSqlQueryModel, public visCore
{
Q_OBJECT

public:
vadTableModel(char *filename=NULL, QObject *parent=NULL);

int rowCount(const QModelIndex &parent=QModelIndex()) const;
int columnCount(const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role=Qt:isplayRole) const;
// QVariant headerData(int section, Qt::Orientation orientation, int role=Qt:isplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index);
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);

private:
/// La table des données.
QVector<int> table;
};

#endif

///////////////////////////////////////////////////////////////////////////////
// File : vadTableModel.cxx //
///////////////////////////////////////////////////////////////////////////////


#include "vadTableModel.h"

static int coords[R*C] = { 25, 25, 75, 25, 50, 75 };

vadTableModel::vadTableModel(char *filename, QObject *parent)
: QSqlQueryModel(parent)
{
if (filename == NULL) {
this->table.resize(R*C);
for (int i=0 ; i<R*C ; i++)
this->table[i] = coords[i];
this->setHeaderData(0, Qt::Horizontal, tr("X"));
this->setHeaderData(1, Qt::Horizontal, tr("Y"));
}
}

int
vadTableModel::rowCount(const QModelIndex &parent) const
{
return R;
}

int
vadTableModel::columnCount(const QModelIndex &parent) const
{
return C;
}

QVariant
vadTableModel::data(const QModelIndex &index, int role) const
{
return this->table[index.row()*C+index.column()];
}

bool
vadTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() < 0 || index.row() >= this->rowCount() ||
index.column() < 0 || index.column() >= this->columnCount())
return FALSE;
this->table[index.row()*C+index.column()] = value.toInt();
emit dataChanged(index, index);
return TRUE;
}

Qt::ItemFlags
vadTableModel::flags(const QModelIndex &index)
{
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}

///////////////////////////////////////////////////////////////////////////////
// File : vadTableView.h //
///////////////////////////////////////////////////////////////////////////////


#ifndef _VAD_TABLE_VIEW_H
#define _VAD_TABLE_VIEW_H

#include <qtableview.h>

class vadTableView : public QTableView
{
Q_OBJECT

public:
vadTableView(QWidget * parent = 0);

protected:
QStyleOptionViewItem viewOptions() const;
};

#endif

///////////////////////////////////////////////////////////////////////////////
// File : vadTableView.cxx //
///////////////////////////////////////////////////////////////////////////////


#include "vadTableView.h"
#include "delegate.h"
//#include "vadTableViewDelegate.h"

vadTableView::vadTableView(QWidget *parent)
: QTableView(parent)
{
//this->setEditTriggers(QAbstractItemView::AllEditTrigger s);
SpinBoxDelegate *delegate = new SpinBoxDelegate();
this->setItemDelegate(delegate);
}

QStyleOptionViewItem
vadTableView::viewOptions() const
{
QStyleOptionViewItem option = QTableView::viewOptions();
option.displayAlignment = Qt::AlignHCenter;
return option;
}