Below is my db model example:
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('book.db')
db.open()
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("card")
self.model.select()
column_names = [ ]
How to get all column header names into a list from qsqltablemodel table?
Based on the the API documentation (for the C++ version), the record()
function can be used to obtain an empty record, for the purpose of extracting the field names (reference):
It returns an empty record, having only the field names. This function can be used to retrieve the field names of a record.
From the record, we can obtain the number of fields and then query the name of each field in a list comprehension expression:
record = model.record()
column_names = [ record.fieldName(i) for i in range(record.count()) ]