QSqlQueryModel rolenames

Phil Thompson phil at riverbankcomputing.com
Sun Feb 26 14:13:23 GMT 2023


On 14/02/2023 21:47, John Plummer wrote:
>     In Qt5/Qt6 the column names of the SQL query in an QSqlQueryModel
> can be accessed using 'record' .  For example the column names can be
> added as roleNames with the following:
> 
> QHash<int, QByteArray> roleNames() const
>     {
>         QHash<int, QByteArray> roles;
>         for (int i = 0; i < record().count(); i ++) {
>             roles.insert(Qt::UserRole + i + 1, 
> record().fieldName(i).toUtf8());
>         }
>         roles.insert(Qt::DisplayRole, "display" );
>         return roles;
>     }
> 
>     Is the 'record' (or column names) available in PyQt6?

Yes. Run...

>>> from PyQt6.QtSql import QSqlQueryModel
>>> help(QSqlQueryModel.record)

Phil


More information about the PyQt mailing list