Results 1 to 4 of 4

Thread: show User Defined Header Labels in QTableView

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default show User Defined Header Labels in QTableView

    Hello every one i have a QTableView in which i am displaying data from the sql database.. I am stuck at a small problem i wanted to display Header names for all the channel's which i have appended in a QStringList. I tried many things but i get the database table column name's as the header's.

    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(tableView);
    2. model->setQuery("SELECT "+ colNames +" from thdata where rowid = (select max(rowid) from thdata)");
    3. tableView->setModel(model);
    4. tableView->showMaximized();
    5. tableView->setAlternatingRowColors(true);
    6.  
    7. //tableView->setHorizontalHeader(thermoNames);
    8. //tableView->setHorizontalHeaderLabels(QString(thermoNames));
    9.  
    10. tableView->resizeRowsToContents();
    11. tableView->resizeColumnsToContents();
    12. timeinterval.clear();
    13. tableView->verticalHeader()->setVisible(false);
    14. colNames.clear();
    To copy to clipboard, switch view to plain text mode 
    this is what i am doing to display all the channels data from the database table, but the header is the column names in the Table i wanted to display the header names which i have defines and stored in a QString thermoNames; , Pls help me out.

    Thank you
    Last edited by nagabathula; 10th August 2011 at 19:14.

  2. #2
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: show User Defined Header Labels in QTableView

    I wanted to put the Header Names which i have defined on the Top Horizontal Header, instead of the horizontal header names which are column names of the sql table. . i have all the names which i want in a QStringList. i looked a lot but din find a solution. .. can any one pls tell me how to go about it.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Post Re: show User Defined Header Labels in QTableView

    setHorizontalHeaderLabels requires a QStringList, furthermore make sure you set the labels after the model is populated. And I guess you have to set the labels everytime you change the query.

    EDIT: or make it, like the documentation tells:
    Qt Code:
    1. model->setQuery("SELECT name, salary FROM employee");
    2. model->setHeaderData(0, Qt::Horizontal, tr("Name"));
    3. model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    4.  
    5. QTableView *view = new QTableView;
    6. view->setModel(model);
    7. view->show();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Lykurg for this useful post:

    nagabathula (11th August 2011)

  5. #4
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: show User Defined Header Labels in QTableView

    Hello Lykurg your example helped me solve my problem.. This is what i did to display the Header Labels which i have defined.

    Qt Code:
    1. for(int i=0; i<thermoNames.count(); i++)
    2. {
    3. model->setHeaderData( i, Qt::Horizontal,thermoNames.at(i));
    4. }
    To copy to clipboard, switch view to plain text mode 

    thank you

Similar Threads

  1. Cannot see the header labels SQL
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2011, 01:38
  2. How to get user-defined message?
    By ponponfish in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2011, 16:19
  3. Replies: 10
    Last Post: 6th April 2011, 10:05
  4. Using User Defined Libraries
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 19:47
  5. User Defined Signal?
    By rajeshs in forum Newbie
    Replies: 2
    Last Post: 18th December 2007, 11:42

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.