Qt: QHeaderView place sort-indicator 在 header 文字的右边

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

如果我设置:QHeaderView::down-arrow { subcontrol-position: center left},down-arrow在列的左边,如果我设置center right,它在列的右边,但是我想将箭头放在标题旁边(右侧)。

您需要设置subcontrol-origin: margin | border | padding | content;

查看以下文档 link 以了解框模型(其中解释了边距矩形、边框矩形、填充矩形和内容矩形)。

http://doc.qt.io/qt-5/stylesheet-customizing.html#the-box-model

因此请尝试将 subcontrol-origin:padding 添加到您的代码中,它可能会添加到您的内容旁边。

尝试如下操作:

QHeaderView::down-arrow { subcontrol-origin:padding; subcontrol-position: center right;}

我排序的方式是为页眉创建一个 QProxyStyle,并覆盖 drawControl。 我还分配了一个空图标来隐藏默认图标。

treeviewwidget.cpp 在初始化():

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