Qt:QHeaderView在标题文本的右侧放置排序指示符

时间:2017-11-06 19:28:35

标签: qt qtreeview qheaderview

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

enter image description here

2 个答案:

答案 0 :(得分:2)

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

查看下面的文档链接,了解框模型(解释边距矩形,边框矩形,填充矩形和内容矩形)。

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

答案 1 :(得分:0)

我排序的方法是为标题创建一个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);
}