QSqlQueryModel::data() not called with Qt::TextAlignmentRole?
-
I have
QSqlQueryModel(and other SQL model types) withQTableViewto 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
ItemDataRoleofQt::TextAlignmentRolefor aligning the data in the table column. Then I could returnQt::AlignmentFlag::AlignRightto 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
QSqlQueryModeland not some other model? DoesQTableViewsimply 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.
-
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
QTableViewattached to the model is doing the drawing, right? Just for example, for all I know it might be thatQTableViewhandles 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::TextAlignmentRoleis 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. -
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.
-
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
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
passstatement.Turns out, that
passbreakpoint is never hit! :( I think I have broken onpassin 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 :)