View Full Version : Get Column Name from a QSqlQueryModel

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


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

15th November 2007, 00: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);

15th November 2007, 00: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).