텍스트를 가운데에 배치하려면이 특정 스타일 동작을 만들기 위해 자신의 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);
@laurapons이 헤더 스타일을 적용 : 당신은이 질문에 더 이상 도움이 필요합니까? – Simon
이 코드를 (qtreewidget에서 상속 한 위젯이 이미 모델/대리자와 프록시를 사용하여) 통합하는 방법을 잘 모르겠습니다. 빠른 응답을 보내 주셔서 감사합니다. – laurapons
필자도 프로젝트에서 모델 뷰 구조를 사용했고 시도해 보았고 작동 했으므로 통합하려면 별도의 클래스'HeaderProxyStyle'을 만든 다음 'm_treeview'를 사용하는 범위에 전용 멤버'HeaderProxyStyle m_oHeaderStyle'을 추가하고 스타일을 헤더'm_treeview-> header() -> setStyle (& m_oHeaderStyle);에 적용하면 효과가있다. 당신도 내가 당신을 도울 수있는 더 많은 코드를 게시 할 수 있습니다. – Simon