PDA

View Full Version : Change initial number of QTableView Vertical Header



guidupas
12th June 2014, 14:56
Hello all

I need to change the initial number of a QTableView vertical header to start with 0, not with 1

How can I do that?

Thanks

anda_skoa
12th June 2014, 15:27
QAbstractItemModel::headerData()

Cheers,
_

guidupas
12th June 2014, 18:45
Hello anda_skoa, thank you for your reply. I didnt understand. Could you give am example?

anda_skoa
12th June 2014, 23:07
The header data, for columns and rows, is delivered by the model, just like the content of the cells.

The default implementation of a table model returns numbers beginnig at 1 for the vertical direction header.
Your model needs to return the row value it self.

You can do that by either modifying the model or using a QIdentityProxyModel that does that and use your actual model at the proxy's source model.

Cheers,
_

guidupas
17th June 2014, 14:04
Solved



for(int i = 0; i < modelo->rowCount(); i++)
{
modelo->setHeaderData(i, Qt::Vertical, i);
}

jefftee
19th June 2014, 01:30
Solved



for(int i = 0; i < modelo->rowCount(); i++)
{
modelo->setHeaderData(i, Qt::Vertical, i);
}

What was suggested by anda_skoa was to override your model's headerData() method. The snippet below would return a header value for each row that is the row number minus 1 for each vertical header:


QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
{
if (orientation == Qt::Vertical && role == Qt::DisplayRole)
return QString::number(section - 1);

// add logic for your horizontal headers here, etc else just return an empty QVariant

return QVariant();
}

I'm not sure where in your code you are setting all of the header data items, but if you have lots of rows, the headerData implementation will perform much better and is cleaner IMHO.

Good luck,

Jeff