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. QTreeView header text alignment
Forum Updated to NodeBB v4.3 + New Features

QTreeView header text alignment

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.8k Views 2 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.
  • myUsernameM Offline
    myUsernameM Offline
    myUsername
    wrote on last edited by
    #1

    Hello everybody!

    I'm facing this strange problem: as you see in the image below, I'm working on a tree view and I cannot set the proper alignment on the header text. (On Qt 5.15.2)

    I am using:

    ui->treeView->header()->setDefaultAlignment(Qt::AlignBottom | Qt::AlignRight);
    

    I am also increasing the default height of the lines and this is showing the issue clearer.
    You can find my full code here: https://gitlab.com/lknth/myFileManager

    c686af96-345d-4a68-ae39-38786c665849-image.png

    How can I fix this?

    Thanks in advance for your help!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The view's default alignment is only used if the model doesn't provide alignment for the header.
      You need to implement the Qt::TextAlignmentRole in your headerData() to either return the alignment you want or an empty QVariant, in which case the view's default will be used

      QVariant MyFileSystemProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
      {
          if (role == Qt::TextAlignmentRole)
              return QVariant();
      
          return QSortFilterProxyModel::headerData(section, orientation, role);
      }
      
      1 Reply Last reply
      4
      • myUsernameM Offline
        myUsernameM Offline
        myUsername
        wrote on last edited by
        #3

        @Chris-Kawa said in QTreeView header text alignment:

        QVariant MyFileSystemProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
        if (role == Qt::TextAlignmentRole)
        return QVariant();

        return QSortFilterProxyModel::headerData(section, orientation, role);
        

        }

        Thank you very much! It works!

        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