Search code examples
qtqtreeviewqheaderview

Qt: QHeaderView place sort-indicator on the right of the header text


If I set: QHeaderView::down-arrow { subcontrol-position: center left}, the down-arrow is on the left of the column, and if I set center right, it is placed on the right of the column, but I want to place the arrow next to the title (on the right side).

enter image description here


Solution

  • The way I sorted was by creating a QProxyStyle for the header, and override drawControl. I also assigned an empty icon to hide the default one.

    treeviewwidget.cpp at initialize():

    treeviewHeaderProxy* m_oHeaderStyle = new treeviewHeaderProxy();
    treeview->header()->setStyle(m_oHeaderStyle);
    treeview->header()->setDefaultAlignment(Qt::AlignCenter);
    treeview->header()->setStyleSheet("QHeaderView::down-arrow { image: url(:/shared/empty); }"
                                    "QHeaderView::up-arrow { image: url(:/shared/empty); } ");
    

    treeviewHeaderProxy.h:

    class treeviewHeaderProxy : public QProxyStyle
    {
    public:
        explicit treeviewHeaderProxy();
        void drawControl(ControlElement oCtrElement, const QStyleOption * poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const;
    
    };
    

    treeviewHeaderProxy.cpp:

    void treeviewHeaderProxy::drawControl(ControlElement oCtrElement, const QStyleOption *poStyleOptionption, QPainter *poPainter, const QWidget *poWidget) const
    {
        // Header label?
        if (oCtrElement == CE_HeaderLabel) {
            QStyleOptionHeader *poStyleOptionHeader = (QStyleOptionHeader *) poStyleOptionption;
            QStyleOptionHeader::SortIndicator sortOption = poStyleOptionHeader->sortIndicator;
            QRect oRect = poStyleOptionHeader->rect;
    
            // Text
            int iTextWidth = poStyleOptionHeader->fontMetrics.width(poStyleOptionHeader->text);
            int iTextHeight = poStyleOptionHeader->fontMetrics.height();
            QRect oTextRect = QRect(oRect.left() + oRect.width(), catRect.top() + (oRect.height() - iTextHeight)/2,
                                    iTextWidth*1.2, iTextHeight);
            poPainter->setPen(SUPER_DARK_GREY);
            poPainter->drawText(oTextRect, poStyleOptionHeader->text); // Draw text
    
            // Sort Indicator
            QPixmap oSortPixmap;
            switch(sortOption){
            case QStyleOptionHeader::SortDown:
                oSortPixmap = QIcon(":/shared/drop_up_grey").pixmap(10,10);
                break;
            case QStyleOptionHeader::SortUp:
                oSortPixmap = QIcon(":/shared/drop_down_grey").pixmap(10,10);
                break;
            }
    
            if(!oSortPixmap.isNull()){
                QRect oSortRect = QRect(oTextRect.left() + oTextRect.width(), oRect.top() + (oRect.height() - oSortPixmap.height())/2,
                                        oSortPixmap.width(), oSortPixmap.height());
                poPainter->drawPixmap(oSortRect, oSortPixmap); // Draw sortIcon
            }
            return;
        }
        QProxyStyle::drawControl(oCtrElement, poStyleOptionption, poPainter, poWidget);
    }