Hoping someone can help me - I've spent a few days searching but haven't had any luck...
I'm using PyQt, and a few qTableViews to display some data - now I want to align this data in the centre.
My code is below - ui_manual_matching.table_unmatched_flights is the qTableView object.
Thanks very much!
Adam
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(settings.str_combined_database)
db.open()
#CREATE AN SQL STRING TO FILTER BY (I wont bore you with the details)
flights_table_model = QSqlTableModel()
flights_table_model.setTable('eg_table')
flights_table_model.setFilter(str_eg_filter)
flights_table_model.select()
ui_manual_matching.table_unmatched_flights.setModel(flights_table_model)
'
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With