설정하면 QHeaderView::down-arrow { subcontrol-position: center left}
, 아래쪽 화살표는 열 왼쪽에 있고 center right
으로 설정하면 열 오른쪽에 있습니다 ,하지만 제목 옆에 화살표를 놓고 싶습니다 (오른쪽에).Qt : QHeaderView 머리글 텍스트의 오른쪽에 정렬 표시기를 배치합니다.
0
A
답변
0
방법은 헤더에 대한 QProxyStyle을 만들어, 그리고 drawControl 우선합니다. 빈 아이콘을 지정하여 기본 아이콘을 숨 깁니다.
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);
}
초기화()에서
1
당신은 subcontrol-origin: margin | border | padding | content
를 설정해야합니다;
상자 모델을 이해하려면 아래 문서 링크를 참조하십시오 (여백 직사각형, 경계 사각형, 패딩 사각형 및 내용 사각형 설명).
http://doc.qt.io/qt-5/stylesheet-customizing.html#the-box-model
그래서 콘텐츠 옆에 추가 할 수 있습니다 코드에subcontrol-origin:padding
를 추가하려고합니다. 다음과 같은
시도 뭔가 : I 정렬
QHeaderView::down-arrow { subcontrol-origin:padding; subcontrol-position: center right;}
작동하지 않습니다. 화살표가 오른쪽에 있습니다. 화살표의 중심은 오른쪽 모서리에 있으므로 화살표의 절반 만 볼 수 있습니다 ... 하위 제어 원점을 사용하고 가능성 값을 변경하여 차이점이 있는지 확인하려고 시도했지만 어떤 효과가있다. – laurapons