kerim
11th April 2011, 13:45
hello,
i have implemented a custom model which was subclassed from QAbstractTableModel.
now i would like to know how to set the headers for specific rows to certain values.
i tried
bool set = ui->tableViewSource->model()->setHeaderData(0, Qt::Vertical, QString("12"));
just right after the model and view were glued together (model contains data).
but return value is false !?!
my model implementation looks like:
#include "tablemodel.h"
// //////////////////////////////////////////////
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
// //////////////////////////////////////////////
{
p2Data = NULL;
}
// //////////////////////////////////////////////
TableModel::TableModel(QStringList *p2Contents,
QObject *parent) : QAbstractTableModel(parent)
// //////////////////////////////////////////////
{
p2Data = p2Contents;
}
// //////////////////////////////////////////////
int TableModel::rowCount(const QModelIndex &parent) const
// //////////////////////////////////////////////
{
Q_UNUSED(parent);
if( !p2Data )
return 0;
return p2Data->size();
}
// //////////////////////////////////////////////
int TableModel::columnCount(const QModelIndex &parent) const
// //////////////////////////////////////////////
{
Q_UNUSED(parent);
return 1;
}
// //////////////////////////////////////////////
QVariant TableModel::data(const QModelIndex &index, int role) const
// //////////////////////////////////////////////
{
if( !p2Data )
return QVariant();
if (!index.isValid())
return QVariant();
if (index.row() >= (int)p2Data->size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return QVariant((*p2Data)[index.row()]);
}
return QVariant();
}
// //////////////////////////////////////////////
bool TableModel::insertRows(int position,
int rows,
const QModelIndex &index)
// //////////////////////////////////////////////
{
if(!p2Data)
return false;
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; row++)
{
p2Data->push_back("");
}
endInsertRows();
return true;
}
// //////////////////////////////////////////////
bool TableModel::removeRows(int position,
int rows,
const QModelIndex &index)
// //////////////////////////////////////////////
{
if(!p2Data)
return false;
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; ++row)
{
p2Data->erase(p2Data->begin()+position);
}
endRemoveRows();
return true;
}
so its some kind of (n, 1) table.
anyone some clue why this is !?!?
i have implemented a custom model which was subclassed from QAbstractTableModel.
now i would like to know how to set the headers for specific rows to certain values.
i tried
bool set = ui->tableViewSource->model()->setHeaderData(0, Qt::Vertical, QString("12"));
just right after the model and view were glued together (model contains data).
but return value is false !?!
my model implementation looks like:
#include "tablemodel.h"
// //////////////////////////////////////////////
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
// //////////////////////////////////////////////
{
p2Data = NULL;
}
// //////////////////////////////////////////////
TableModel::TableModel(QStringList *p2Contents,
QObject *parent) : QAbstractTableModel(parent)
// //////////////////////////////////////////////
{
p2Data = p2Contents;
}
// //////////////////////////////////////////////
int TableModel::rowCount(const QModelIndex &parent) const
// //////////////////////////////////////////////
{
Q_UNUSED(parent);
if( !p2Data )
return 0;
return p2Data->size();
}
// //////////////////////////////////////////////
int TableModel::columnCount(const QModelIndex &parent) const
// //////////////////////////////////////////////
{
Q_UNUSED(parent);
return 1;
}
// //////////////////////////////////////////////
QVariant TableModel::data(const QModelIndex &index, int role) const
// //////////////////////////////////////////////
{
if( !p2Data )
return QVariant();
if (!index.isValid())
return QVariant();
if (index.row() >= (int)p2Data->size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return QVariant((*p2Data)[index.row()]);
}
return QVariant();
}
// //////////////////////////////////////////////
bool TableModel::insertRows(int position,
int rows,
const QModelIndex &index)
// //////////////////////////////////////////////
{
if(!p2Data)
return false;
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; row++)
{
p2Data->push_back("");
}
endInsertRows();
return true;
}
// //////////////////////////////////////////////
bool TableModel::removeRows(int position,
int rows,
const QModelIndex &index)
// //////////////////////////////////////////////
{
if(!p2Data)
return false;
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; ++row)
{
p2Data->erase(p2Data->begin()+position);
}
endRemoveRows();
return true;
}
so its some kind of (n, 1) table.
anyone some clue why this is !?!?