Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeView header text alignment



  • 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!


  • Moderators

    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);
    }
    


  • @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!


Log in to reply