Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQueryModel::data() not called with Qt::TextAlignmentRole?
Forum Updated to NodeBB v4.3 + New Features

QSqlQueryModel::data() not called with Qt::TextAlignmentRole?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #1

    I have QSqlQueryModel (and other SQL model types) with QTableView 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 of Qt::TextAlignmentRole for aligning the data in the table column. Then I could return Qt::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 for Qt::TextAlignmentRole.

    Is that right? Is that because it's a QSqlQueryModel and not some other model? Does QTableView simply not do it that way? Or what? :)

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Christian-Ehrlicher

        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 that QTableView 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.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          See QStyledItemDelegate::initStyleOption(): https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp.html#287
          So you must be doing something wrong.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 2 Replies Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            See QStyledItemDelegate::initStyleOption(): https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp.html#287
            So you must be doing something wrong.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Christian-Ehrlicher Thanks v. much, I will investigate and report back.

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              See QStyledItemDelegate::initStyleOption(): https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qstyleditemdelegate.cpp.html#287
              So you must be doing something wrong.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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 on pass 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 :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved