Solved QSqlQueryModel::data() not called with Qt::TextAlignmentRole?
-
I have
QSqlQueryModel
(and other SQL model types) withQTableView
to view it.Because I am already overriding http://doc.qt.io/qt-5/qsqlquerymodel.html#data, I was expecting to find that called at some point with
ItemDataRole
ofQt::TextAlignmentRole
for aligning the data in the table column. Then I could returnQt::AlignmentFlag::AlignRight
to align my numbers right.I have seen this suggested & accepted as simplest solution at least for Qt 4. But under my Qt 5.7 I can only say that
QSqlQueryModel::data()
never does seem to get called forQt::TextAlignmentRole
.Is that right? Is that because it's a
QSqlQueryModel
and not some other model? DoesQTableView
simply not do it that way? Or what? :) -
Since QSqlQueryModel is just a QAbstractItemModel, data() will also be called with Qt::TextAlignmentRole when an item is drawn. Therefore you must be doing something wrong. Please show us some compileable code.
-
will also be called with Qt::TextAlignmentRole when an item is drawn
And that will definitely apply when a
QTableView
attached to the model is doing the drawing, right? Just for example, for all I know it might be thatQTableView
handles any alignment in cells itself, and hence would never need to call with the alignment role....My proof is that, as I say, I have overridden the
virtual QSqlQueryModel::data()
and examined calls in debugger/debug output. I see many, many calls to that method which must be coming from the drawer/tableview, with a variety of roles passed in requesting various attributes for the data. However,Qt::TextAlignmentRole
is not one of the roles I ever see. Hence the question.I shall have to think about what code to post here, as obviously this is all one part of a complete project. I do not mind doing further investigating myself before doing so. Hence my question to discover what the behaviour should be, before I spend my time.
-
See QStyledItemDelegate::initStyleOption(): https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp.html#287
So you must be doing something wrong. -
@Christian-Ehrlicher Thanks v. much, I will investigate and report back.
-
@Christian-Ehrlicher
For my sins, I have to develop in Python/PyQt. I use the (excellent) PyCharm IDE/debugger.To test it would be called before writing the actual code, I had:
def data(self, index: QtCore.QModelIndex, role: QtCore.Qt.ItemDataRole = QtCore.Qt.DisplayRole) -> typing.Any: if role == QtCore.Qt.TextAlignmentRole: pass return super().data(index, role)
and I placed a breakpoint on the
pass
statement.Turns out, that
pass
breakpoint is never hit! :( I think I have broken onpass
in other circumstances, and it was. Anyway, I shall not be using that for breakpoints any more!So you are correct, and I have no problem now :)