2011-11-18 118 views
1

这里的主要问题是:我使用QTreeWidget类作为主树,它必须告诉我这个树状结构:如何以不同风格绘制QTreeWidgetItem?

[Today] 
    [Row1] 
    [Row2] 
     [SubRow21] 
     [SubRow22] 
    [Row3] 
[Yesterday] 
    [Row4] 
     [SubRow41] 
[etc] 

有了Qt Designer我已经将这个样式表代码:

QTreeWidget#treeWidget::item 
{ 
    height: 24px;  
    border: none;  
    background-position: bottom left; 
    background-image: url(:/backgrounds/images/backgrounds/row_back.png); 
} 
QTreeWidget#treeWidget::item:selected 
{ 
    color: #000000;  
    background-position: bottom left; 
    background-image: url(:/spreadsheet/images/spreadsheet/row_back_selected.png); 
} 

而且所有项目与* row_back.png *背景图片,我需要绘制另一背景图像[户拉y] and [昨天]行!出于这些目的,我不得不继承QStyledItemDelegate类,但不知道方法,重置风格的新背景图片:

void MyColumnDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 
{ 
    QStyleOptionViewItemV4 op = option; 

    if(isPeriod(index)) 
    { 
      // here I heed to change background image!!! 

      //QStyle* style = op.widget ? op.widget->style() : QApplication::style(); 
      //style->drawControl(QStyle::CE_ItemViewItem, &op, painter, op.widget); 

      //QRect backgroundRect(option.rect.x() + 5, option.rect.y() + 5, 18, 18); 
      //style->drawItemPixmap(painter, backgroundRect, Qt::AlignCenter, QPixmap(SOME_PIC));  
      //QStyledItemDelegate::paint(painter, op, index); 

      return; 
    } 

    op.text = ""; 
    QStyle* style = op.widget ? op.widget->style() : QApplication::style(); 
    style->drawControl(QStyle::CE_ItemViewItem, &op, painter, op.widget); 

    QModelIndex dataIndex = index.model()->index(index.row(), ePosChain, index.parent()); 
    LetterInfo data = index.model()->data(dataIndex, PackageDataRole).value<LetterInfo>(); 

    switch(data.document_.state_) 
    { 
    case(eStateSignedByOwner): 
      painter->drawPixmap(getPos(op, index, QPixmap(IMAGE2)), QPixmap(IMAGE2)); 
      break; 
    case(eStateSignedByHost): 
      painter->drawPixmap(getPos(op, index, QPixmap(IMAGE1)), QPixmap(IMAGE1)); 
      break; 
    } 

    painter->drawText(getTextPos(op, index), data.text); 
} 

任何帮助吗?

谢谢!

回答

1

解决与此代码:

if(isPeriod(index)) 
    { 
      QPixmap pixmap(PERIOD_ROW_BACKGROUND); 
      painter->drawPixmap(op.rect, pixmap, pixmap.rect()); 

      return; 
    }