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. How to align data in QTableView?
Forum Updated to NodeBB v4.3 + New Features

How to align data in QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k Views
  • 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.
  • C Offline
    C Offline
    CuriousPan
    wrote on last edited by
    #1

    Hello. I have my table view with QAbstractTableModel and I want to align all the data to the right. How can I do it?
    I found several tutorials on this topic, but all of them reimplement data() method which I use for returning data of the model. Am I misunderstanding the method or is there a way to do it in some another way?

    JonBJ 1 Reply Last reply
    0
    • C CuriousPan

      Hello. I have my table view with QAbstractTableModel and I want to align all the data to the right. How can I do it?
      I found several tutorials on this topic, but all of them reimplement data() method which I use for returning data of the model. Am I misunderstanding the method or is there a way to do it in some another way?

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

      @CuriousPan
      You are misunderstanding the full power/usage of QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const. Go look at that, and at enum Qt::ItemDataRole.

      Qt infrastructure calls the data() method multiple times to render each data item. It does so by passing in different values for the role parameter. Your job is to write a switch statement looking at roles value and return different types of information. When it is called with the (default) Qt::DisplayRole (or Qt::EditRole), and only then, you should return your item's data value. When it is called with Qt::TextAlignmentRole you should return your desired right-alignment value.

      1 Reply Last reply
      4
      • C Offline
        C Offline
        CuriousPan
        wrote on last edited by
        #3

        @JonB. Thank you for the answer. I defenitely should read the documentation better.
        Now I have one more question - I align data to the right, but it's in the right edge. Is there a way to align it to the right and vertically to the center?
        8839a2eb-f901-4ae0-a722-2c155da97a87-image.png

        C 1 Reply Last reply
        0
        • C CuriousPan

          @JonB. Thank you for the answer. I defenitely should read the documentation better.
          Now I have one more question - I align data to the right, but it's in the right edge. Is there a way to align it to the right and vertically to the center?
          8839a2eb-f901-4ae0-a722-2c155da97a87-image.png

          C Offline
          C Offline
          CuriousPan
          wrote on last edited by
          #4

          @CuriousPan, found an answer to my question. Thank you.
          58c15721-baef-4c7f-95f4-cb38ccc0c55b-image.png

          JonBJ 1 Reply Last reply
          1
          • C CuriousPan

            @CuriousPan, found an answer to my question. Thank you.
            58c15721-baef-4c7f-95f4-cb38ccc0c55b-image.png

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

            @CuriousPan
            :)

            While you are looking at enum Qt::ItemDataRole, see what other roles are defined. You can affect fonts, colors, tooltips.... You can also look at the passed-in QModelIndex and/or the type of QVariant returned from Qt::DisplayRole/EditRole to selectively return different values for these, e.g. only numbers should be right-aligned, strings should be left-aligned.

            1 Reply Last reply
            1

            • Login

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