Results 1 to 5 of 5

Thread: setHeaderData Qt:Vertical on QSqlTableModel

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default setHeaderData Qt:Vertical on QSqlTableModel

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setHeaderData Qt:Vertical on QSqlTableModel

    Could you show us what you've tried?
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: setHeaderData Qt:Vertical on QSqlTableModel

    I tried a proxyModel like this :
    Qt Code:
    1. QProxyModel *proxy = new QProxyModel;
    2. proxy->setModel(mySqlTableModel);
    3. proxy->setHeaderData(0, Qt::Vertical, "test", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. bool SqlTableModel::setHeaderData(int section, Qt::Orientation orientation,
    2. const QVariant & value, int role)
    3. {
    4. if(QAbstractItemModel::setHeaderData(section, orientation, value, role)){
    5. emit headerDataChanged(orientation, section, section);
    6. return true;
    7. }
    8. else
    9. {
    10. return false;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. mySqlTableModel->setHeaderData(1, Qt::Vertical, "Summe", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    returns false

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: setHeaderData Qt:Vertical on QSqlTableModel

    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().
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    janus (9th August 2008)

  6. #5
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: setHeaderData Qt:Vertical on QSqlTableModel

    thx jpn

    it works

    Qt Code:
    1. QVariant SqlTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if(orientation == Qt::Vertical && role == Qt::DisplayRole){
    4. return QVariant("test");
    5. }
    6. else{
    7. return QSqlTableModel::headerData(section, orientation, role);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by janus; 10th August 2008 at 11:02.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.