PDA

View Full Version : setHeaderData Qt:Vertical on QSqlTableModel



janus
9th August 2008, 12:29
Hi,

i would like to set a row name instead of the default row number in my SqlTableModel which is a subclass of QSqlTableModel. I tried a proxyModel with (proxy->setHeaderData(.., Qt::Vertical, ..)) and reimplemented setHeaderData in my SqlTablemodel class (AbstractItemModel::setHeaderData(...)) ... both ways did not work. Does anybody know how to achieve this?

thx janus

jpn
9th August 2008, 16:34
Could you show us what you've tried?

janus
9th August 2008, 18:36
I tried a proxyModel like this :


QProxyModel *proxy = new QProxyModel;
proxy->setModel(mySqlTableModel);
proxy->setHeaderData(0, Qt::Vertical, "test", Qt::DisplayRole);

second I added this code to SqlTableModel (subclass of QSqlTableModel).


bool SqlTableModel::setHeaderData(int section, Qt::Orientation orientation,
const QVariant & value, int role)
{
if(QAbstractItemModel::setHeaderData(section, orientation, value, role)){
emit headerDataChanged(orientation, section, section);
return true;
}
else
{
return false;
}
}


mySqlTableModel->setHeaderData(1, Qt::Vertical, "Summe", Qt::DisplayRole);

returns false

jpn
9th August 2008, 19:09
QAbstractItemModel doesn't store header data. It's just an interface to any data. You'll need to store the header data to a data structure of some kind and return it in headerData().

janus
10th August 2008, 10:53
thx jpn

it works :)


QVariant SqlTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Vertical && role == Qt::DisplayRole){
return QVariant("test");
}
else{
return QSqlTableModel::headerData(section, orientation, role);
}
}