2013-03-05 59 views
1

我有QTableView派生视图的QStyledItemDelegate派生对象。我根据模型索引数据类型进一步委托绘画和编辑器创建。对于bool s,我想通过复选框来表示状态 - 但复选框从不出现。无法在QStyledItemDelegate中绘制复选框

这是基础的委托漆功能:

void Sy_QtPropertyDelegate::paint(QPainter* painter, 
            const QStyleOptionViewItem& option, 
            const QModelIndex& index) const 
{ 
    painter->save(); 

    if (index.column() == 0) { 
     ... 
    } else { 
     QVariant var = index.data(); 
     bool modified = index.data(Sy_QtPropertyModel::ModifiedRole).toBool(); 

     // If the data type is one of our delegates, then push the work onto 
     // that. 
     auto it = delegateMap_.find(var.type()); 
     if (it != delegateMap_.end()) { 
      (*it)->paint(painter, option, index); 
     } else if (var.type() != QVariant::UserType) { 
      ... 
     } else { 
      ... 
     } 
    } 

    painter->restore(); 
} 

而且bool子委托漆功能:

void Sy_boolPD::paint(QPainter* painter, 
         const QStyleOptionViewItem& option, 
         const QModelIndex& index) const 
{ 
    painter->save(); 

    bool checked = index.data().toBool(); 
    bool modified = index.data(Sy_QtPropertyModel::ModifiedRole).toBool(); 

    QStyle* style = Sy_application::style(); 
    if (modified) { 
     QStyleOptionViewItemV4 bgOpt(option); 
     bgOpt.backgroundBrush = QBrush(Sy_QtPropertyDelegate::ModifiedColour); 
     style->drawControl(QStyle::CE_ItemViewItem, &bgOpt, painter); 
    } 

    QStyleOption butOpt(option); 
    butOpt.state = QStyle::State_Enabled; 
    butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off; 
    style->drawControl(QStyle::CE_CheckBox, &butOpt, painter); 

    painter->restore(); 
} 

如果我强迫modified是真实的,背景是表的颜色cout ing butOptrectstate成员显示他们是正确的 - 但没有显示复选框!将QStyle::CE_CheckBox设置为任何其他类型也会导致无法呈现。

我已经和Qt的MVC框架合作过很多次,但是我看不出这里出了什么问题。

回答

0

我所要做的就是看在源代码

case CE_CheckBox: 
    if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(option)) { 
    ... 
    } 

QStyleOption我传递给该方法被转换为具体绘制控件的类型,CE_CheckBox需要QStyleOptionButton,如果演员失败的绘画操作默默跳过。