2017-11-06 15 views
0

QTreeView 위젯의 일부 열에 대해서는 아이콘을 사용합니다. 아이콘이Qt : QTreeView : 머리글의 가운데 아이콘

QVariant headerData (int section, Qt::Orientation orientation, int role) const{ 
    if(role == Qt::DecorationRole) 
    { 
     QIcon icon; 
     if (section == 0) { 
      icon.addFile(":/icon1"); 
     } else if (section == 1){ 
      icon.addFile(":/icon2"); 
     } 
    } 

    if(role == Qt::TextAlignmentRole) 
    { 
     return (Qt::AlignLeft + Qt::AlignVCenter); 
    } 

에 설정되어있는 헤더는 다음과 같습니다 : I 텍스트와 아이콘을 정렬 할

enter image description here

. TextAlignmentRole은 텍스트에서만 작동하지만 아이콘에서는 작동하지 않습니다. 어떻게해야합니까?

또한 기본 정렬을 설정하여 시도 :

m_treeview->header()->setDefaultAlignment(Qt::AlignCenter);하지만 행운을.

답변

1

텍스트를 가운데에 배치하려면이 특정 스타일 동작을 만들기 위해 자신의 proxy style을 구현해야합니다.

#include <QProxyStyle> 
#include <QPainter> 

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

}; 
텍스트 구현

센터 아이콘

void HeaderProxyStyle::drawControl(ControlElement oCtrElement, const QStyleOption *poStylrOptionption, QPainter *poPainter, const QWidget *poWidget) const 
    { 
     // Header label? 
     if (oCtrElement == CE_HeaderLabel) { 
      // YES - Allocate style option header 
      QStyleOptionHeader *poStyleOptionHeader = 
        (QStyleOptionHeader *) poStylrOptionption; 

      // Get header icon 
      QIcon oIcon = qvariant_cast<QIcon>(poStyleOptionHeader->icon); 

      // Icon is valid? 
      if(oIcon.isNull()){ 
       // No - Draw text header 
       QProxyStyle::drawControl(oCtrElement, poStylrOptionption, poPainter, poWidget); 
       return; 
      } 

      // Set icon size 16x16 
      QSize oIconSize = QSize(16,16); 

      // Get header section rect 
      QRect oRect = poStyleOptionHeader->rect; 

      // Create header icon pixmap 
      QPixmap oIconPixmap = oIcon.pixmap(oIconSize.width(),oIconSize.height()); 

      // Calculate header text width 
      int iTextWidth = poStyleOptionHeader->fontMetrics.width(poStyleOptionHeader->text); 

      QRect oCenterRec = QRect(oRect.left(), 
            oRect.top() + (oRect.height - iTextSize)/2, 
            oIconPixmap.width(),oIconPixmap.height()); 


      QRect oTextRect = QRect(oCenterRec.left()+ oIconSize.width(), 
           oCenterRec.top(), oCenterRec.width() + iTextWidth, oCenterRec.height()); 
      // Draw icon 
      poPainter->drawPixmap(oCenterRec, oIconPixmap); 
      // Draw text 
      poPainter->drawText(oTextRect, poStyleOptionHeader->text); 
      return; 
     } 
     QProxyStyle::drawControl(oCtrElement, poStylrOptionption, poPainter, poWidget); 

    } 

는 그런 다음 트리보기

// Set header style 
m_treeview->header()->setStyle(&m_oHeaderStyle); 
+0

@laurapons이 헤더 스타일을 적용 : 당신은이 질문에 더 이상 도움이 필요합니까? – Simon

+0

이 코드를 (qtreewidget에서 상속 한 위젯이 이미 모델/대리자와 프록시를 사용하여) 통합하는 방법을 잘 모르겠습니다. 빠른 응답을 보내 주셔서 감사합니다. – laurapons

+0

필자도 프로젝트에서 모델 뷰 구조를 사용했고 시도해 보았고 작동 했으므로 통합하려면 별도의 클래스'HeaderProxyStyle'을 만든 다음 'm_treeview'를 사용하는 범위에 전용 멤버'HeaderProxyStyle m_oHeaderStyle'을 추가하고 스타일을 헤더'm_treeview-> header() -> setStyle (& m_oHeaderStyle);에 적용하면 효과가있다. 당신도 내가 당신을 도울 수있는 더 많은 코드를 게시 할 수 있습니다. – Simon