You should be able to achieve this by subclassing QSqlTableModel
and overriding the data()
method. Something like this should do the trick
class MySqlModel(QSqlTableModel):
def data(self, index, role=Qt.DisplayRole):
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
return QSqlTableModel.data(self,index,role)
This will preserve the behaviour of the QSqlTableModel
you have now, except that when the view asks for the text alignment, your model returns the alignment you want. You can of course choose the alignment by using one of the other flags (see the Qt documentation) and even add more logic to set different alignments based on the model index passed into the data()
method.
P.S. Don't forget to instantiate this new model in your code instead of the one you use now. So replace flights_table_model = QSqlTableModel()
with flights_table_model = MySqlModel()