PDA

View Full Version : show User Defined Header Labels in QTableView



nagabathula
10th August 2011, 19:02
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.



QSqlQueryModel *model = new QSqlQueryModel(tableView);
model->setQuery("SELECT "+ colNames +" from thdata where rowid = (select max(rowid) from thdata)");
tableView->setModel(model);
tableView->showMaximized();
tableView->setAlternatingRowColors(true);

//tableView->setHorizontalHeader(thermoNames);
//tableView->setHorizontalHeaderLabels(QString(thermoNames));

tableView->resizeRowsToContents();
tableView->resizeColumnsToContents();
timeinterval.clear();
tableView->verticalHeader()->setVisible(false);
colNames.clear();
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

nagabathula
10th August 2011, 21:11
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.

Lykurg
10th August 2011, 22:11
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:
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT name, salary FROM employee");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));

QTableView *view = new QTableView;
view->setModel(model);
view->show();

nagabathula
11th August 2011, 06:38
Hello Lykurg your example helped me solve my problem.. This is what i did to display the Header Labels which i have defined.


for(int i=0; i<thermoNames.count(); i++)
{
model->setHeaderData( i, Qt::Horizontal,thermoNames.at(i));
}


thank you