PDA

View Full Version : Get Column Name from a QSqlQueryModel



sgmurphy19
14th November 2007, 22:50
How do you get the name of a column from a QSqlQueryModel if all I have is the index?

Thanks!

jacek
14th November 2007, 22:52
You can use QSqlQueryModel::record() or headerData() for this.

sgmurphy19
14th November 2007, 23:09
Thanks, I got caught up looking in the wrong place.
If anyone else ever asks, I used:
int row = 0;
int column = 0;
QString columnName = model->record(row).fieldName(column);

jacek
14th November 2007, 23:20
It's a bit more efficent to use the record() method without parameters, since you don't need a record populated with data:

QString columnName = model->record().fieldName( column );
The version with headerData() looks like this:

QString columnName = model->headerData( column, Qt::Horizontal ).toString();
(although in some cases it might not return the original name of the column).