PDA

View Full Version : setting header for specific rows within QTableView with custom model



kerim
11th April 2011, 14: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 !?!?

wysota
11th April 2011, 14:57
You didn't implement setHeaderData() for your model.

kerim
11th April 2011, 16:00
hmmm ...
using this:

// //////////////////////////////////////////////
bool TableModel::setHeaderData ( int section,
Qt::Orientation orientation,
const QVariant & value,
int role )
// //////////////////////////////////////////////
{
QAbstractTableModel::setHeaderData(section,orienta tion,value,role);
emit headerDataChanged(Qt::Vertical,0,1);
}
it still does not work.
just for the case their is no misunderstanding:
i intend to change the line number for first line to "12" (just an example)

i hope wysota will be kind enough to show me the trees within this forrest because i cant see em :confused:

wysota
11th April 2011, 18:21
using this:
it still does not work.
Why would it work? Especially that you don't return any value from this method.



i intend to change the line number for first line to "12" (just an example)
Tell that to your model. You need to make headerData() return "12" for the appropriate input parameters. QAbstractTableModel will not do that by itself.

viulskiez
11th April 2011, 19:41
In this case, you still have to put the "headers data" in a data structures then reimplement headerData and setHeaderData.

kerim
12th April 2011, 10:03
thnx all 4 the hints ..
of course the last posted code fragment missed a return statement,
but actually implementing setHeaderData(..) is not necessary in my case
because returning the appropriate return-value within headerData(..) is enough.

thnx alot, i am learning alot within this forum.
greets.